2023年7月15日发(作者:)
Android 2D 绘图--------Drawable类的用法
一. 从项目资源来创建图像
1. Java程序代码内使用方法建立窗体布局
代码:
public class MDrawableActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
te(savedInstanceState);
LinearLayout mLinearLayout = new
LinearLayout(this);
ImageView mImageView = new ImageView(this);
geResource(ua);
ustViewBounds(true);
outParams(new
Params(_CONTENT,
_CONTENT));
w(mImageView);
setContentView(mLinearLayout);
}
} 2. 通过Resources 类来获得项目资源
Resources res = getResources();
Drawable drawable = wable();
二. 从XML文件来定义图像
1) 可在XML文件定义的绘图子类
绘图子类 XML文件内的标签名称
AnimationDrawable(动画绘图)
BitmapDrawable(图像绘图)
ClipDrawble(剪切绘图)
ColorDrawable(色彩绘图)
GradientDrawable(斜度绘图)
InsetDrawable(插入绘图)
LevelListDrawable
RotateDrawable(旋转绘图)
ScaleDrawable(缩放绘图)
StateListDrawable
TransitionDrawable(过度绘图)
2)定义图像的xml文件(以TransitionDrawable为例)
xmlns:android=/apk/res/android>
代码:
public class TransitionDrawableActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState) {
te(savedInstanceState);
setContentView();
Resources res = getResources();
TransitionDrawable transition =
(TransitionDrawable)wable(tion_image); //图片自动从第一张慢慢淡化到第二张。
ImageView mImageView =
(ImageView)findViewById(View);
geDrawable(transition);
ransition(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());
nt().setColor(0xff74AC23);
nds(x ,y , x+width, y+height);
}
protected void onDraw(Canvas canvas) {
(canvas);
}
}
然后在Activity里面显示
public class ShapDrawableActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
te(savedInstanceState);
MShapeDrawable mShapDrawable = new
MShapeDrawable(this);
setContentView(mShapDrawable);
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689425750a246222.html
评论列表(0条)