欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Bitmap and DrawPoint

程序员文章站 2022-04-20 08:57:53
...
[align=center][b][size=large]Bitmap and DrawPoint[/size][/b][/align]
Android UI 中有时需要画Bitmap,那么如何画Bitmap?
[list]
[*]利用已有资源初始化Bitmap。
[/list]

public void fillPointArrays(int color,Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(pointSize, pointSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, pointSize, pointSize);
drawable.draw(canvas);
pointArray[color] = bitmap;
}

Resources r = this.getContext().getResources();
fillPointArrays(GREEN,r.getDrawable(R.drawable.green_point));



[list]
[*]屏幕触摸画点,利用已经初始化的Bitmap画点。
[/list]

@Override
public boolean onTouchEvent(MotionEvent event) {
//获得触摸点的坐标
point = newPoint(event.getX(), event.getY());

refressCanvas();
return true;
}

onTouchEvent(MotionEvent event) 重载了View()的函数,用来监听屏幕的触摸,从event事件中可以获得触摸点的坐标。

[list]
[*]根据触摸点的坐标,画出触摸的点。
[/list]
    //画点
private void drawPoint(Canvas canvas){
canvas.drawBitmap(pointArray[0],point.x*pointSize+xOffset,point.y*pointSize+yOffset,paint);
}

//根据触摸点坐标找到对应点
private Point newPoint(Float x, Float y){
Point p = new Point(0, 0);
for (int i = 0; i < maxX; i++) {
if ((i * pointSize + xOffset) <= x
&& x < ((i + 1) * pointSize + xOffset)) {
p.setX(i);
}
}
for (int i = 0; i < maxY; i++) {
if ((i * pointSize + yOffset) <= y
&& y < ((i + 1) * pointSize + yOffset)) {
p.setY(i);
}
}
return p;


}
相关标签: Android UI