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 = 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 类来获得项目资源
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.xxx);
二. 从XML文件来定义图像
1) 可在XML文件定义的绘图子类
绘图子类 XML文件内的标签名称
AnimationDrawable(动画绘图) BitmapDrawable(图像绘图) ClipDrawble(剪切绘图) ColorDrawable(色彩绘图) GradientDrawable(斜度绘图) InsetDrawable(插入绘图) LevelListDrawable RotateDrawable(旋转绘图) ScaleDrawable(缩放绘图) StateListDrawable TransitionDrawable(过度绘图) 2)定义图像的xml文件(以TransitionDrawable为例) 代码: public class TransitionDrawableActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); 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对象 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); mShapDrawable.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); } } 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- stra.cn 版权所有 赣ICP备2024042791号-4
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务