Android中的13种Drawable资源类型
Android支持13种不同类型的Drawable资源,它们分别是:Bitmap、Nine-Patch Bitmap、Shape Drawable、Gradient Drawable、Color Drawable、State List Drawable、Level List Drawable、Scale Drawable、Clip Drawable、Inset Drawable、Animation Drawable、Layer List Drawable和Group Drawable。
1. Bitmap
Bitmap是最常见的Drawable资源,是一种位图,可以使用它以位图的形式显示图像,它可以从文件、资源或者流中获取。使用它可以设置抗锯齿、调整大小等属性。
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image); ImageView myImage = (ImageView) findViewById(R.id.my_image_view); myImage.setImageBitmap(bitmap);
2. Nine-Patch Bitmap
Nine-Patch Bitmap是一种特殊类型的Bitmap,它可以拉伸以适应不同屏幕大小的设备,它可以按照指定的规则拉伸,而不会影响图像的质量。
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_ninepatch); NinePatchDrawable ninePatchDrawable = new NinePatchDrawable(bitmap, bitmap.getNinePatchChunk(), new Rect(), null); ImageView myImage = (ImageView) findViewById(R.id.my_image_view); myImage.setImageDrawable(ninePatchDrawable);
3. Shape Drawable
Shape Drawable是一种可以绘制形状的Drawable,它可以用来绘制矩形、圆形、椭圆等形状,还可以设置填充颜色、边框等属性。
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape()); shapeDrawable.getPaint().setColor(Color.RED); shapeDrawable.setIntrinsicWidth(100); shapeDrawable.setIntrinsicHeight(100); ImageView myImage = (ImageView) findViewById(R.id.my_image_view); myImage.setImageDrawable(shapeDrawable);
4. Gradient Drawable
Gradient Drawable是一种可以绘制渐变色的Drawable,它可以绘制线性渐变、径向渐变和扫描渐变,还可以设置填充颜色、边框等属性。
GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[]{Color.RED, Color.BLUE}); gradientDrawable.setShape(GradientDrawable.RECTANGLE); gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT); gradientDrawable.setCornerRadius(20); ImageView myImage = (ImageView) findViewById(R.id.my_image_view); myImage.setImageDrawable(gradientDrawable);
5. Color Drawable
Color Drawable是一种可以设置颜色的Drawable,它可以用来设置某一个View的背景色,也可以用来设置图片的背景色。
ColorDrawable colorDrawable = new ColorDrawable(Color.RED); ImageView myImage = (ImageView) findViewById(R.id.my_image_view); myImage.setBackgroundDrawable(colorDrawable);
6. State List Drawable
State List Drawable是一种特殊类型的Drawable,它可以根据控件的状态来设置不同的Drawable,它可以根据控件的状态(比如按下、聚焦、选中等)来设置不同的Drawable,从而实现状态切换的效果。
StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(Color.RED)); stateListDrawable.addState(new int[]{android.R.attr.state_focused}, new ColorDrawable(Color.BLUE)); stateListDrawable.addState(new int[]{}, new ColorDrawable(Color.GREEN)); ImageView myImage = (ImageView) findViewById(R.id.my_image_view); myImage.setBackgroundDrawable(stateListDrawable);
7. Level List Drawable
Level List Drawable是一种特殊类型的Drawable,它可以根据控件的等级来设置不同的Drawable,它可以根据控件的等级(比如进度条的进度)来设置不同的Drawable,从而实现等级切换的效果。
LevelListDrawable levelListDrawable = new LevelListDrawable(); levelListDrawable.addLevel(0, 0, new ColorDrawable(Color.RED)); levelListDrawable.addLevel(1, 1, new ColorDrawable(Color.BLUE)); levelListDrawable.addLevel(2, 2, new ColorDrawable(Color.GREEN)); ImageView myImage = (ImageView) findViewById(R.id.my_image_view); myImage.setBackgroundDrawable(levelListDrawable);
8. Scale Drawable
Scale Drawable是一种特殊类型的Drawable,它可以根据控件的大小来设置不同的Drawable,它可以根据控件的大小(比如缩放比例)来