首页上一页 1 下一页尾页 1 条记录 1/1页
跟随手指的小兔子
发表在Android图书答疑
2015-10-14
是否精华
是
否
版块置顶:
是
否
android从入门到精通
63页3.1.4开发自定义的view,例子是光盘tm/sl/3/3.4,案例里边只有一个兔子,我又新建了两只兔子,为什么只能选中第一只兔子,后面的兔子选不中呢,不能移动后面的兔子
自定义view
public class view extends View {
public float bitmapx;
public float bitmapy;
public view(Context context) {
super(context);
bitmapx=50;
bitmapy=20;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint pa=new Paint();
Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(bitmap, bitmapx,bitmapy, pa);
if(bitmap.isRecycled()){
bitmap.recycle();
}
}
}
-----------------------------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout fr =(FrameLayout) findViewById(R.id.id);
final view mv=new view(MainActivity.this);
mv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
mv.bitmapx=arg1.getX();
mv.bitmapy=arg1.getY();
mv.invalidate();
return true;
}
});
fr.addView(mv);
}
63页3.1.4开发自定义的view,例子是光盘tm/sl/3/3.4,案例里边只有一个兔子,我又新建了两只兔子,为什么只能选中第一只兔子,后面的兔子选不中呢,不能移动后面的兔子
自定义view
public class view extends View {
public float bitmapx;
public float bitmapy;
public view(Context context) {
super(context);
bitmapx=50;
bitmapy=20;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint pa=new Paint();
Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(bitmap, bitmapx,bitmapy, pa);
if(bitmap.isRecycled()){
bitmap.recycle();
}
}
}
-----------------------------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout fr =(FrameLayout) findViewById(R.id.id);
final view mv=new view(MainActivity.this);
mv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
mv.bitmapx=arg1.getX();
mv.bitmapy=arg1.getY();
mv.invalidate();
return true;
}
});
fr.addView(mv);
}