今天我们继续介绍Android平台底层绘图类的相关内容,在Android UI开发专题(一) 之界面设计中我们介绍了有关Android平台资源使用以及Bitmap相关类的操作,接下来将会以实例的方式给大家演示各种类的用处以及注意点。今天我们继续了解android.graphics包中比较重要的绘图类。 一、 android.graphics.Matrix 有关图形的变换、缩放等相关操作常用的方法有: void reset() // 重置一个matrix对象。 void set(Matrix src) //复制一个源矩阵,和本类的构造方法 Matrix(Matrix src) 一样 boolean isIdentity() //返回这个矩阵是否定义(已经有意义) void setRotate(float degrees) //指定一个角度以0,0为坐标进行旋转 void setRotate(float degrees, float px, float py) //指定一个角度以px,py为坐标进行旋转 void setScale(float sx, float sy) // 缩放 void setScale(float sx, float sy, float px, float py) //以坐标px,py进行缩放 […]
UI开发
Android UI开发专题(三) 各种Drawable Android开发技术
本次我们主要讲解Android平台下的各种Drawable,这里在SDK的android.graphics.drawable包下面可以看到有各种Drawable类多达十几种,它们到底之间有什么关系和区别呢? 一、AnimationDrawable 顾名思义该类主要表示动画的图形类,可以实现逐帧播放的效果,下面代码示例如下 1. 定义一个cwj_animation.xml 放到res/drawable 目录下,其中定义的属性duration为延时,单位为毫秒,而oneshot属性表示是否仅播放一次,内容为: <animation-list android:id="selected" android:oneshot="false"><item android:drawable="@drawable/cwj0" android:duration="30" /><item android:drawable="@drawable/cwj1" android:duration="30" /><item android:drawable="@drawable/cwj2" android:duration="30" /><item android:drawable="@drawable/cwj3" android:duration="30" /><item android:drawable="@drawable/cwj4" android:duration="30" /><item android:drawable="@drawable/cwj5" android:duration="30" /></animation-list> 2.在java中调用也很简单 ImageView img = (ImageView)findViewById(R.id.cwj_image); //首先声明一个ImageView对象在xml布局文件中 img.setBackgroundResource(R.drawable.cwj_animation); //我们刚才的animation定义的xml文件 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); //构造AnimationDrawable对象 frameAnimation.start() //开始播放动画 3. AnimationDrawable类还提供了一些常用的方法如下: void stop() 停止 void addFrame(Drawable frame, int duration) 添加一帧,类似xml中的布局 Drawable getFrame(int index) […]
Android UI开发专题(五) Bitmap和Canvas实例 Android开发技术
在Android UI开发专题的前五节我们讲到的东西主要是基础和理论内容,从本次Android123将通过实例代码来演示,本次主要是Bitmap和Canvas类的使用,根据要求缩放Bitmap对象并返回新的Bitmap对象。centerToFit方法一共有4个参数,返回一个Bitmap类型,第一个参数为原始的位图对象,width和height分别为新的宽和高,而Context是用来加载资源的上下文实例。 Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) { final int bitmapWidth = bitmap.getWidth(); //获取原始bitmap的宽度 final int bitmapHeight = bitmap.getHeight(); if (bitmapWidth < width || bitmapHeight < height) { int color = context.getResources().getColor(R.color.window_background); //从资源读取背景色 Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth, bitmapHeight < height ? height […]
GraphableButton类实现Android UI开发 Android开发技术
从Android 1.6开始,系统设置中的电池使用记录提供了一种简单的自绘Button按钮演示-GraphableButton类,通过GraphableButton我们可以很清晰的了解到前几次Android123讲到的UI开发要点。 public class GraphableButton extends Button { //从Button类继承 private static final String TAG = "GraphableButton"; static Paint[] sPaint = new Paint[2]; //定义两种颜色 static { sPaint[0] = new Paint(); sPaint[0].setStyle(Paint.Style.FILL); sPaint[0].setColor(0xFF0080FF); sPaint[1] = new Paint(); sPaint[1].setStyle(Paint.Style.FILL); sPaint[1].setColor(0xFFFF6060); } double[] mValues; public GraphableButton(Context context, AttributeSet attrs) { super(context, attrs); […]

