您可以直接修改位图,也可以在位图SurfaceView
上放置一个并在SurfaceView
. 在这种情况下,您所要做的就是onDraw
在SurfaceView
.
我在我的Turtle Draw应用程序中画了很多线......这是我扩展的方式SurfaceView
public class DrawView extends SurfaceView {
Paint paint = new Paint();
List < float[] > lines = new ArrayList < float[] > ();
List < Integer > colors = new ArrayList < Integer > ();
int curColor = Color.WHITE;
int bgColor = Color.BLACK;
Bitmap mBitmap;
ImageView turtle;
float curX, curY, curTurn = 0f;
Matrix transform = new Matrix();
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
mBitmap = getBitmapFromDrawable(context);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(DpiUtils.getPxFromDpi(getContext(), 2));
setScrollContainer(true);
// clear();
}
public DrawView(Context context) {
super(context);
}
public void addLine(float...l) {
synchronized(lines) {
lines.add(l);
colors.add(curColor);
}
}
public List < float[] > getLines() {
return lines;
}
public List < Integer > getColors() {
return colors;
}
@Override
public void onDraw(final Canvas canvas) {
synchronized(lines) {
super.onDraw(canvas);
int i = 0;
for (float[] l: lines) {
paint.setAntiAlias(true);
paint.setColor(colors.get(i++));
canvas.drawLines(l, paint);
curX = l[2];
curY = l[3];
}
transform.setTranslate(curX - 13, curY - 13);
transform.preRotate(360 - curTurn, 13, 13);
paint.setColor(Color.BLACK);
canvas.drawBitmap(mBitmap, transform, paint);
}
}
public void setTurn(float turn) {
this.curTurn = turn;
}
public void clear() {
lines.clear();
colors.clear();
DisplayMetrics metrics = DpiUtils.getDisplayMetrics(getContext());
curX = metrics.widthPixels / 2f;
curY = (metrics.heightPixels / 2f) - DpiUtils.getPxFromDpi(getContext(), 50);
curTurn = 0;
scrollTo(0, 0);
}
public static Bitmap getBitmapFromAsset(Context context, String strName) {
AssetManager assetManager = context.getAssets();
InputStream istr;
Bitmap bitmap = null;
try {
istr = assetManager.open(strName);
bitmap = BitmapFactory.decodeStream(istr);
} catch (IOException e) {
return null;
}
return bitmap;
}
public static Bitmap getBitmapFromDrawable(Context context) {
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.turtle_26);
return icon;
}
public void setDrawColor(int color) {
paint.setColor(color);
curColor = color;
}
public int getDrawColor() {
return curColor;
}
int x, y = 0;
int scrollByX, scrollByY = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = (event.getAction() & MotionEvent.ACTION_MASK);
if (action == MotionEvent.ACTION_DOWN) {
x = (int) event.getX() + scrollByX;
y = (int) event.getY() + scrollByY;
} else if (action == MotionEvent.ACTION_MOVE) {
scrollByX = x - (int) event.getX();
scrollByY = y - (int) event.getY();
scrollTo(scrollByX, scrollByY);
}
return true;
}
@Override
public void scrollTo(int x, int y) {
// TODO Auto-generated method stub
super.scrollTo(x, y);
scrollByX = x;
scrollByY = y;
}
@Override
public void setBackgroundColor(int color) {
// TODO Auto-generated method stub
super.setBackgroundColor(color);
bgColor = color;
}
public int getBackgroundColor() {
return bgColor;
}
}