《Android Drawable的用法.docx》由会员分享,可在线阅读,更多相关《Android Drawable的用法.docx(4页珍藏版)》请在三一办公上搜索。
1、Android Drawable的用法Android 2D 绘图-Drawable类的用法 一 从项目资源来创建图像 1. Java程序代码内使用方法建立窗体布局 代码: public class MDrawableActivity extends Activity Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); LinearLayout mLinearLayout = new LinearLayout(this); ImageView mImageView
2、 = new ImageView(this); mImageView.setImageResource(R.drawable.baxinhua); mImageView.setAdjustViewBounds(true); mImageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); mLinearLayout.addView(mImageView); setContentView(mLinearLayout); 2. 通过Resources
3、类来获得项目资源 Resources res = getResources; Drawable drawable = res.getDrawable(R.drawable.xxx); 二 从XML文件来定义图像 1) 可在XML文件定义的绘图子类 绘图子类 XML文件内的标签名称 AnimationDrawable(动画绘图) BitmapDrawable(图像绘图) ClipDrawble(剪切绘图) ColorDrawable(色彩绘图) GradientDrawable(斜度绘图) InsetDrawable(插入绘图) (层次绘图) LevelListDrawable RotateDr
4、awable(旋转绘图) ScaleDrawable(缩放绘图) StateListDrawable TransitionDrawable(过度绘图) 2)定义图像的xml文件 transition xmlns:android= 代码: public class TransitionDrawableActivity extends Activity Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main);
5、Resources res = getResources; TransitionDrawable transition = (TransitionDrawable)res.getDrawable(R.drawable.transition_image); /图片自动从第一张慢慢淡化到第二张。 ImageView mImageView = (ImageView)findViewById(R.id.mImageView); mImageView.setImageDrawable(transition); transition.startTransition(5*1000); 2) 自定义View对
6、象 public class MShapeDrawable extends View private ShapeDrawable mShapDrawable; public MShapeDrawable(Context context) super(context); int x = 10; int y = 10; int width = 300; int height = 50; mShapDrawable = new ShapeDrawable(new OvalShape); mShapDrawable.getPaint.setColor(0xff74AC23); mShapDrawabl
7、e.setBounds(x ,y , x+width, y+height); protected void onDraw(Canvas canvas) mShapDrawable.draw(canvas); 然后在Activity里面显示 public class ShapDrawableActivity extends Activity Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); MShapeDrawable mShapDrawable = new MShapeDrawable(this); setContentView(mShapDrawable);