我想画上的图片我已经采取。 绘画作品没有绘制的图片,但如果我绘制位图我只看到了位,但ñ绘图出现。 我尝试了很多,但似乎没有任何帮助。 先谢谢了。
私有类MyView的扩展视图实现OnTouchListener {
文件根= Environment.getExternalStorageDirectory();
路径道路;
ArrayList的<路径> _graphics =新的ArrayList<路径>();
位图MYBITMAP;
涂料myPaint;
公共MyView的(上下文的背景下){
超(上下文);
档案文件=新的文件(根,temp.jpg);
MYBITMAP = NULL;
如果(file.exists()){
BitmapFactory.Options选项=新BitmapFactory.Options();
options.in preferredConfig = Bitmap.Config.ARGB_8888;
MYBITMAP = setBitmap(BitmapFactory.de codeFILE(新文件(根,
temp.jpg)getPath(),选件))。
}
myPaint =新的油漆();
myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
}
@覆盖
保护无效的OnDraw(帆布油画){
canvas.drawBitmap(MYBITMAP,0,0,NULL);
对于(路径路径:_graphics){
canvas.drawPath(路径,myPaint);
}
}
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
如果(event.getAction()== MotionEvent.ACTION_DOWN){
路径=新路径();
path.moveTo(event.getX(),event.getY());
}否则,如果(event.getAction()== MotionEvent.ACTION_MOVE){
path.lineTo(event.getX(),event.getY());
}否则,如果(event.getAction()== MotionEvent.ACTION_UP){
path.lineTo(event.getX(),event.getY());
_graphics.add(路径);
}
返回true;
}
}
解决方案
公共类drawView函数扩展视图实现OnTouchListener {
私人帆布mCanvas;
私人路径的mpath;
私人油漆mPaint;
私人的ArrayList<路径>路径=新的ArrayList<路径>();
私人的ArrayList<路径> undonePaths =新的ArrayList<路径>();
公共drawView函数(上下文的背景下){
mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
超(上下文);
setFocusable(真正的);
setFocusableInTouchMode(真正的);
this.setOnTouchListener(本);
mPaint =新的油漆();
mPaint.setAntiAlias(真正的);
mPaint.setDither(真正的);
mPaint.setColor(Color.CYAN);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(4);
mCanvas =新的Canvas();
的mpath =新路径();
paths.add(的mpath);
}
@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
super.onSizeChanged(W,H,oldw,oldh);
BitmapFactory.Options选择=新BitmapFactory.Options();
opt.inMutable = TRUE;
位图tempbit = BitmapFactory.de codeFILE(新文件(根,
temp.jpg)getPath(),OPT)。
位图mBitmap = Bitmap.createBitmap(tempbit.getWidth,tempbit.getHeight,Bitmap.Config.ARGB_8888);
mBitmap = Bitmap.createBitmap(tempbit);
mCanvas =新的Canvas(mBitmap);
}
@覆盖
保护无效的OnDraw(帆布油画){
canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
对于(路径P:路径){
canvas.drawPath(对,mPaint);
}
}
私人浮动MX,我的;
私有静态最终浮动TOUCH_TOLERANCE = 4;
私人无效touch_start(浮X,浮动Y){
mPath.reset();
mPath.moveTo(X,Y);
MX = X;
我= Y;
}
私人无效TOUCH_MOVE(浮X,浮动Y){
浮DX = Math.abs(X - MX);
浮DY = Math.abs(Y - 我的);
如果(DX> = TOUCH_TOLERANCE || DY> = TOUCH_TOLERANCE){
mPath.quadTo(MX,MY,(X + MX)/ 2,(Y +我)/ 2);
MX = X;
我= Y;
}
}
私人无效touch_up(){
mPath.lineTo(MX,MY);
//提交路径,我们的屏幕外
mCanvas.drawPath(的mpath,mPaint);
//杀了这个,所以我们不重复抽奖
的mpath =新路径();
paths.add(的mpath);
}
公共无效onClickUndo(){
如果(paths.size()大于0){
undonePaths.add(paths.remove(paths.size() - 1));
无效();
} 其他 {
}
//敬酒用户
}
公共无效onClickRedo(){
如果(undonePaths.size()大于0){
paths.add(undonePaths.remove(undonePaths.size() - 1));
无效();
} 其他 {
}
//敬酒用户
}
公共布尔onTouch(查看为arg0,MotionEvent事件){
浮X = event.getX();
浮动Y = event.getY();
开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:
touch_start(X,Y);
无效();
打破;
案例MotionEvent.ACTION_MOVE:
TOUCH_MOVE(X,Y);
无效();
打破;
案例MotionEvent.ACTION_UP:
润色();
无效();
打破;
}
返回true;
}
I am trying to draw on a picture i have taken. Drawing works without the picture drawn but if i draw the bitmap i only see the bitmap but n drawing comes up. I tried a lot but nothing seems to help. Thanks in advance.
private class myView extends View implements OnTouchListener{
File root = Environment.getExternalStorageDirectory();
Path path;
ArrayList<Path> _graphics = new ArrayList<Path>();
Bitmap myBitmap;
Paint myPaint;
public myView(Context context) {
super(context);
File file = new File(root, "temp.jpg");
myBitmap = null;
if (file.exists()) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
myBitmap = setBitmap(BitmapFactory.decodeFile(new File(root,
"temp.jpg").getPath(), options));
}
myPaint = new Paint();
myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(myBitmap, 0, 0, null);
for (Path path : _graphics) {
canvas.drawPath(path, myPaint);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
path = new Path();
path.moveTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
path.lineTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_UP) {
path.lineTo(event.getX(), event.getY());
_graphics.add(path);
}
return true;
}
}
解决方案
public class DrawView extends View implements OnTouchListener {
private Canvas mCanvas;
private Path mPath;
private Paint mPaint;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
public DrawView(Context context) {
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.CYAN);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(4);
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);
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
Bitmap tempbit = BitmapFactory.decodeFile(new File(root,
"temp.jpg").getPath(),opt);
Bitmap mBitmap=Bitmap.createBitmap(tempbit.getWidth, tempbit.getHeight, Bitmap.Config.ARGB_8888);
mBitmap=Bitmap.createBitmap(tempbit);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
for (Path p : paths) {
canvas.drawPath(p, mPaint);
}
}
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 void onClickUndo() {
if (paths.size() > 0) {
undonePaths.add(paths.remove(paths.size() - 1));
invalidate();
} else {
}
// toast the user
}
public void onClickRedo() {
if (undonePaths.size() > 0) {
paths.add(undonePaths.remove(undonePaths.size() - 1));
invalidate();
} else {
}
// toast the user
}
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;
}