首页 文章 精选 留言 我的

精选列表

搜索[android],共10000篇文章
优秀的个人博客,低调大师

Android中Bitmap,byte[],Drawable相互转化

一、相关概念 1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象 2、Canvas画布,绘图的目的区域,用于绘图 3、Bitmap位图,用于图的处理 4、Matrix矩阵 二、Bitmap 1、从资源中获取Bitmap Resourcesres=getResources(); Bitmapbmp=BitmapFactory.decodeResource(res,R.drawable.icon); 2、Bitmap → byte[] []Bitmap2Bytes(Bitmapbm){ ByteArrayOutputStreambaos=ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG,100,baos); baos.toByteArray(); } 3、byte[] → Bitmap BitmapBytes2Bimap([]b){ (b.length!=0){ BitmapFactory.decodeByteArray(b,0,b.length); }{ ; } } 4、Bitmap缩放 BitmapzoomBitmap(Bitmapbitmap,width,height){ w=bitmap.getWidth(); h=bitmap.getHeight(); Matrixmatrix=Matrix(); scaleWidth=(()width/w); scaleHeight=(()height/h); matrix.postScale(scaleWidth,scaleHeight); Bitmapnewbmp=Bitmap.createBitmap(bitmap,0,0,w,h,matrix,); newbmp; } 5、将Drawable转化为Bitmap BitmapdrawableToBitmap(Drawabledrawable){ w=drawable.getIntrinsicWidth(); h=drawable.getIntrinsicHeight(); Bitmap.Configconfig=drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888 :Bitmap.Config.RGB_565; Bitmapbitmap=Bitmap.createBitmap(w,h,config); Canvascanvas=Canvas(bitmap); drawable.setBounds(0,0,w,h); drawable.draw(canvas); bitmap; } 6、获得圆角图片 BitmapgetRoundedCornerBitmap(Bitmapbitmap,roundPx){ w=bitmap.getWidth(); h=bitmap.getHeight(); Bitmapoutput=Bitmap.createBitmap(w,h,Config.ARGB_8888); Canvascanvas=Canvas(output); color=0xff424242; Paintpaint=Paint(); Rectrect=Rect(0,0,w,h); RectFrectF=RectF(rect); paint.setAntiAlias(); canvas.drawARGB(0,0,0,0); paint.setColor(color); canvas.drawRoundRect(rectF,roundPx,roundPx,paint); paint.setXfermode(PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap,rect,rect,paint); output; } 7、获得带倒影的图片 BitmapcreateReflectionImageWithOrigin(Bitmapbitmap){ reflectionGap=4; w=bitmap.getWidth(); h=bitmap.getHeight(); Matrixmatrix=Matrix(); matrix.preScale(1,-1); BitmapreflectionImage=Bitmap.createBitmap(bitmap,0,h/2,w, h/2,matrix,); BitmapbitmapWithReflection=Bitmap.createBitmap(w,(h+h/2), Config.ARGB_8888); Canvascanvas=Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap,0,0,); PaintdeafalutPaint=Paint(); canvas.drawRect(0,h,w,h+reflectionGap,deafalutPaint); canvas.drawBitmap(reflectionImage,0,h+reflectionGap,); Paintpaint=Paint(); LinearGradientshader=LinearGradient(0,bitmap.getHeight(),0, bitmapWithReflection.getHeight()+reflectionGap,0x70ffffff, 0x00ffffff,TileMode.CLAMP); paint.setShader(shader); paint.setXfermode(PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0,h,w,bitmapWithReflection.getHeight() +reflectionGap,paint); bitmapWithReflection; } 三、Drawable 1、Bitmap转换成Drawable Bitmapbm=xxx;BitmapDrawablebd=BitmapDrawable(getResource(),bm); 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。 2、Drawable缩放 DrawablezoomDrawable(Drawabledrawable,w,h){ width=drawable.getIntrinsicWidth(); height=drawable.getIntrinsicHeight(); Bitmapoldbmp=drawableToBitmap(drawable); Matrixmatrix=Matrix(); sx=(()w/width); sy=(()h/height); matrix.postScale(sx,sy); Bitmapnewbmp=Bitmap.createBitmap(oldbmp,0,0,width,height, matrix,); BitmapDrawable(newbmp); } 本文转自dyh7077063的博客http://dyh7077063.iteye.com/blog/970672 本文转自wauoen51CTO博客,原文链接:http://blog.51cto.com/7183397/1604493,如需转载请自行联系原作者

优秀的个人博客,低调大师

[Unity3d for android]屏幕触摸事件

移动物体: [csharp] view plain copy usingUnityEngine; usingSystem.Collections; publicclassexample:MonoBehaviour{ publicfloatspeed=0.1F; voidUpdate(){ if(Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Moved){ Vector2touchDeltaPosition=Input.GetTouch(0).deltaPosition; transform.Translate(-touchDeltaPosition.x*speed,-touchDeltaPosition.y*speed,0); } } } 点击碰撞克隆 [csharp] view plain copy usingUnityEngine; usingSystem.Collections; publicclassexample:MonoBehaviour{ publicGameObjectprojectile; voidUpdate(){ inti=0; while(i<Input.touchCount){ if(Input.GetTouch(i).phase==TouchPhase.Began) clone=Instantiate(projectile,transform.position,transform.rotation)asGameObject; ++i; } } } =================== [csharp] view plain copy usingUnityEngine; usingSystem.Collections; publicclassexample:MonoBehaviour{ publicGameObjectparticle; voidUpdate(){ inti=0; while(i<Input.touchCount){ if(Input.GetTouch(i).phase==TouchPhase.Began){ Rayray=Camera.main.ScreenPointToRay(Input.GetTouch(i).position); if(Physics.Raycast(ray)) Instantiate(particle,transform.position,transform.rotation)asGameObject; } ++i; } } } TouchPhaseEnumeration Describes phase of a finger touch. Values Began A finger touched the screen. Moved A finger moved on the screen. Stationary A finger is touching the screen but hasn't moved. Ended A finger was lifted from the screen. This is the final phase of a touch. Canceled The system cancelled tracking for the touch, as when (for example) the user puts the device to her face or more than five touches happened simultaneously. This is the final phase of a touch. 本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366234,如需转载请自行联系原作者

优秀的个人博客,低调大师

LayoutParams继承于Android.View.ViewGroup.LayoutParams.

LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。 可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。 但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值: 1,一个确定的值; 2,FILL_PARENT,即填满(和父容器一样大小); 3,WRAP_CONTENT,即包裹住组件就好。 在JAVA中动态构建的布局,常常这样写: setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 上面这一句话其实是子对父的,也就是说,父布局下的子控件要设置这句话。 因为布局很多,虽然都继承至ViewGroup但是各个布局还是有很大的不同。 很显然上面这句应该这样写才算准确: setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.FILL_PARENT)); 这表示这个子控件的父布局是一个TableRow , 这样的LayoutParams 太多,所以应明确指明。 下面分别说下两个常用到布局: 1. FrameLayout下动态设置子控件居中,动态用JAVA代码要这样实现: FrameLayout.LayoutParams lytp = new FrameLayout.LayoutParams(80,LayoutParams.WRAP_CONTENT); lytp .gravity = Gravity.CENTER; btn.setLayoutParams(lytp); 2. RelativeLayout下动态设置子控件居中: RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); btn1.setLayoutParams(lp); // 动态初始化小圆点,便于代码的扩展。 for (int i = 0; i < mImageIds.length; i++) { View point = new View(this); point.setBackgroundResource(R.drawable.shape_point_gray);// 所有的View都有背景, LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( 10, 10);//宽高为10像素,因为圆点的父控件是LinearLayout所以这里用LinearLayout.LayoutParams, if (i > 0) { params.leftMargin = 10;// 设置圆点间隔 } point.setLayoutParams(params);// 设置圆点的大小 llPointGroup.addView(point);// 将圆点动态添加给线性布局 } 本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5497880.html,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码