android 在自己绘制view时,怎么在类中加入滚动条,不用配置文件
答案:2 悬赏:30 手机版
解决时间 2021-02-10 18:19
- 提问者网友:最爱你的唇
- 2021-02-10 06:54
android 在自己绘制view时,怎么在类中加入滚动条,不用配置文件
最佳答案
- 五星知识达人网友:神也偏爱
- 2021-02-10 08:15
// 构造一个 RelativeLayout,设置其背景色
al = new RelativeLayout(this);
al.setBackgroundColor(Color.YELLOW);
RelativeLayout.LayoutParams tvLP = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, 30);
ll = new LinearLayout(this);
ll.setId(1);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackgroundColor(Color.GRAY);
Button button = new Button(this);
button.setText("退出");
button.setLayoutParams(LP_WW);
ll.addView(button);
al.addView(ll, tvLP);
v = new DrawPage(this);
RelativeLayout.LayoutParams vLP = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
vLP.addRule(RelativeLayout.PERSISTENT_ALL_CACHES, 1);
// 也可以先为子 View 设置 LayoutParams,然后再调用一个参数的 ViewGroup.addView(View)
// 来添加。效果是一样的。
al.addView(v, vLP);
this.setContentView(al);
}
class DrawPage extends View {
private int scrollX = 0;
private int scrollY = 0;
Paint paint = new Paint();
Bitmap me = null, me1 = null, me2 = null, me3 = null, me4 = null,
me5 = null;
Rect table1 = new Rect(0, 0, 155, 120);
Rect table2 = new Rect(155, 0, 311, 120);
Rect table3 = new Rect(0, 120, 155, 240);
Rect table4 = new Rect(0, 240, 155, 360);
Rect table5 = new Rect(0, 360, 155, 480);
Rect table6 = new Rect(0, 480, 155, 600);
Rect topChair = new Rect(53, 10, 77, 29);
public DrawPage(Context context) {
super(context);
me = loadImage(R.drawable.table4, table1);
me1 = loadImage(R.drawable.table4, table2);
me2 = loadImage(R.drawable.table4, table3);
me3 = loadImage(R.drawable.table4, table4);
me4 = loadImage(R.drawable.table4, table5);
me5 = loadImage(R.drawable.table4, table6);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(me, 0, 0, paint);
canvas.drawBitmap(me1, 0, 0, paint);
canvas.drawBitmap(me2, 0, 0, paint);
canvas.drawBitmap(me3, 0, 0, paint);
canvas.drawBitmap(me4, 0, 0, paint);
canvas.drawBitmap(me5, 0, 0, paint);
}
public Bitmap loadImage(int fileid, Rect rect) {
Resources r = this.getContext().getResources();
Bitmap bitmap = Bitmap.createBitmap(rect.right, rect.bottom,
Config.ARGB_8888);
Drawable drawable = r.getDrawable(fileid);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(rect);
drawable.draw(canvas);
return bitmap;
}
}
al = new RelativeLayout(this);
al.setBackgroundColor(Color.YELLOW);
RelativeLayout.LayoutParams tvLP = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, 30);
ll = new LinearLayout(this);
ll.setId(1);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setBackgroundColor(Color.GRAY);
Button button = new Button(this);
button.setText("退出");
button.setLayoutParams(LP_WW);
ll.addView(button);
al.addView(ll, tvLP);
v = new DrawPage(this);
RelativeLayout.LayoutParams vLP = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
vLP.addRule(RelativeLayout.PERSISTENT_ALL_CACHES, 1);
// 也可以先为子 View 设置 LayoutParams,然后再调用一个参数的 ViewGroup.addView(View)
// 来添加。效果是一样的。
al.addView(v, vLP);
this.setContentView(al);
}
class DrawPage extends View {
private int scrollX = 0;
private int scrollY = 0;
Paint paint = new Paint();
Bitmap me = null, me1 = null, me2 = null, me3 = null, me4 = null,
me5 = null;
Rect table1 = new Rect(0, 0, 155, 120);
Rect table2 = new Rect(155, 0, 311, 120);
Rect table3 = new Rect(0, 120, 155, 240);
Rect table4 = new Rect(0, 240, 155, 360);
Rect table5 = new Rect(0, 360, 155, 480);
Rect table6 = new Rect(0, 480, 155, 600);
Rect topChair = new Rect(53, 10, 77, 29);
public DrawPage(Context context) {
super(context);
me = loadImage(R.drawable.table4, table1);
me1 = loadImage(R.drawable.table4, table2);
me2 = loadImage(R.drawable.table4, table3);
me3 = loadImage(R.drawable.table4, table4);
me4 = loadImage(R.drawable.table4, table5);
me5 = loadImage(R.drawable.table4, table6);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(me, 0, 0, paint);
canvas.drawBitmap(me1, 0, 0, paint);
canvas.drawBitmap(me2, 0, 0, paint);
canvas.drawBitmap(me3, 0, 0, paint);
canvas.drawBitmap(me4, 0, 0, paint);
canvas.drawBitmap(me5, 0, 0, paint);
}
public Bitmap loadImage(int fileid, Rect rect) {
Resources r = this.getContext().getResources();
Bitmap bitmap = Bitmap.createBitmap(rect.right, rect.bottom,
Config.ARGB_8888);
Drawable drawable = r.getDrawable(fileid);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(rect);
drawable.draw(canvas);
return bitmap;
}
}
全部回答
- 1楼网友:十年萤火照君眠
- 2021-02-10 08:32
同问。。。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯