我喜欢制作一个应用程序,就像一个小小的颜料,我必须得到一个位图,在画布上绘制它,接下来,画上它(用闪光灯)...所以,我实际上有这个代码:< / p>
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Environment;
import android.util.Log;
import android.graphics.Bitmap;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class DrawView extends View implements OnTouchListener {
private Canvas mCanvas;
private Path mPath;
public Paint mPaint;
private ArrayList<Path> paths = new ArrayList<Path>();
private boolean start = true;
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener((OnTouchListener) this);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(4);
mPaint.setDither(true);
mPaint.setFilterBitmap(true);
mCanvas = new Canvas();
mPath = new Path();
paths.add(mPath);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
for (Path p : paths) {
canvas.drawPath(p, mPaint);
}
if (start) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.v01);
canvas.drawBitmap(bmp, 0, 0, mPaint);
start = false;
}
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath = new Path();
paths.add(mPath);
}
public boolean onTouch(View arg0, MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
使用此代码,我可以在画布上绘制,并通过第一种方式获取位图...但是,如果我触摸,位图消失,这是每个onTouch事件的原因我再次根据路径绘制...我怎么能做我喜欢的事情呢?我的意思是,得到Bitmap,然后画出来......
答案 0 :(得分:0)
如果将canvas.drawBitmap()移到if语句之外,则应该有效。保持解码,但将实际绘图移出它。
答案 1 :(得分:0)
我不确定你的问题在这里。每次运行onDraw
时都必须重绘位图,否则就不会有任何内容。你有没有注意到那里的性能损失?
您不必做的就是在invalidate
上解码资源。
将bmp声明为全局变量
Bitmap bmp;
将位图存储在构造函数中:
DrawView(Context context){
...
bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.v01);
}
并删除绘制方法中的if。你不希望它在那里。每次调用invalidate时都必须重绘位图。但这不应该导致任何性能问题。
canvas.drawBitmap(bmp, 0, 0, mPaint);
答案 2 :(得分:0)
这会让你的观点变得非常慢,但是如果删除if(start)
语句,那么你的位图就不会消失。
更好的方法是将canvas.drawBitmap()
置于if(start)
方法之外,但这仍然会很慢,因为每次触摸屏幕时都会绘制位图。
实际上这种情况正在发生,因为每次调用onDraw()
时都会传递一个空白位图。这意味着每次调用onDraw时都必须重绘所有内容,但是因为你只是第一次设置位图,所以当再次调用draw时,位图不会再次添加,因此就会消失。
答案 3 :(得分:0)
解决了,我使用这段代码来加载图片并在上面绘图......
DrawView.java
package com.example.com.dibuja;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class DrawView extends View implements OnTouchListener {
private Canvas mCanvas;
private Path mPath;
public Paint mPaint;
private ArrayList<Path> paths = new ArrayList<Path>();
Bitmap bmp;
public DrawView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.v01);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(6);
mCanvas = new Canvas();
mPath = new Path();
paths.add(mPath);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(bmp, 0, 0, mPaint);
for (Path p : paths) {
canvas.drawPath(p, mPaint);
}
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 0;
Draw dw = new Draw();
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath = new Path();
paths.add(mPath);
}
@Override
public boolean onTouch(View arg0, MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}