怎么画的图像,图像保存到手机图库中的android [英] how to paint on an image and save the image to phone gallery in android
本文介绍了怎么画的图像,图像保存到手机图库中的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
**我的目的是为了拯救画图像连同我的背景,以画廊** //
公共布尔onTouch(查看视图,MotionEvent事件){
//绘制新指向我们内部的画布/位图
的System.out.println(onTouch(查看VC){IS CALLING11111111111);
如果(event.getAction()== MotionEvent.ACTION_DOWN){
paint.setColor(Color.RED);
路径=新路径();
path.moveTo(event.getX(),event.getY());
} 其他
如果(event.getAction()== MotionEvent.ACTION_MOVE){
INT historySize = event.getHistorySize();
的for(int i = 0; I< historySize;我++){
path.lineTo(event.getHistoricalX(ⅰ),
event.getHistoricalY(ⅰ));
}
path.lineTo(event.getX(),event.getY());
canvas.drawPath(路径,油漆);
}
其他 {
返回super.onTouchEvent(事件);
}
无效();
返回true;
}
解决方案
请尝试以下工作code 我觉得应该对你有所帮助。
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.Canvas;
进口android.graphics.Matrix;
进口android.graphics.Paint;
进口android.view.MotionEvent;
进口android.view.View;
公共类DrawableImageView扩展视图{
私人位图mBitmap;
私人位图峰;
私人帆布mCanvas;
私人最终涂料mPaint;
私人INT A = 255;
私人诠释R = 255;
私人诠释G = 255;
私人INT B = 255;
私人浮动宽度= 4;
公共DrawableImageView(上下文C,位图图片){
超级(C);
PIC = IMG;
mPaint =新的油漆();
mPaint.setAntiAlias(真正的);
mPaint.setARGB(A,R,G,B);
位图newBitmap = Bitmap.createBitmap(img.getWidth(),img.getHeight(),Bitmap.Config.RGB_565);
帆布newCanvas =新的Canvas();
newCanvas.setBitmap(newBitmap);
如果(IMG!= NULL){
newCanvas.drawBitmap(IMG,0,0,NULL);
}
mBitmap = newBitmap;
mCanvas = newCanvas;
mCanvas.setBitmap(mBitmap);
}
公共DrawableImageView(上下文C,位图IMG,INT阿尔法,诠释红色,诠释绿色,诠释蓝){
这个(C,IMG);
setColor(α,红,绿,蓝);
}
公共DrawableImageView(上下文C,位图IMG,INT阿尔法,诠释红色,诠释绿色,诠释蓝色,浮动W){
这个(C,IMG,alpha,红色,绿色,蓝色);
宽= W;
}
公共位图getBitmap(){返回mBitmap;}
公共无效setWidth(浮点W){宽度= W;}
公共无效setColor(INT阿尔法,诠释红色,诠释绿色,诠释蓝){
A =阿尔法;
R =红色;
G =绿色;
B =蓝色;
mPaint.setARGB(A,R,G,B);
}
公共无效撤消(){
mCanvas.drawBitmap(PIC,0,0,NULL);
无效();
}
浮动的scaleX;
浮动的scaleY;
浮规模;
@覆盖保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
将scaleX =(浮点)W / mBitmap.getWidth();
的scaleY =(浮点)H / mBitmap.getHeight();
规模=的scaleX>的scaleY?的scaleY:将scaleX;
}
@覆盖保护无效的OnDraw(帆布油画){
如果(mBitmap!= NULL){
字模=新的Matrix();
matrix.postScale(秤,秤);
canvas.drawBitmap(mBitmap,矩阵,NULL);
//canvas.drawBitmap(mBitmap,0,0,NULL);
}
}
浮动lastX;
浮动lastY;
@覆盖公共布尔的onTouchEvent(MotionEvent事件){
mPaint.setStrokeWidth(宽/缩放);
浮CURX = event.getX()/刻度;
浮CURY = event.getY()/刻度;
开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:{
mCanvas.drawCircle(CURX,CURY,宽度/ 2 /规模,mPaint);
打破;
}
案例MotionEvent.ACTION_MOVE:{
mCanvas.drawLine(lastX,lastY,CURX,CURY,mPaint);
mCanvas.drawCircle(CURX,CURY,宽度/ 2 /规模,mPaint); //行开始和停线之间的怪异锯齿修复发生
打破;
}
案例MotionEvent.ACTION_CANCEL:
案例MotionEvent.ACTION_UP:{
mCanvas.drawLine(lastX,lastY,CURX,CURY,mPaint);
mCanvas.drawCircle(CURX,CURY,宽度/ 2 /规模,mPaint);
打破;
}
}
lastX = CURX;
lastY = CURY;
无效(); //无效只修改了RECT ...
返回true;
}
}
** my intent is to save the painted image along with my background view to gallery** //
public boolean onTouch(View view, MotionEvent event) {
// draw the new Points to our internal canvas / bitmap
System.out.println("onTouch(View vc) { IS CALLING11111111111");
if (event.getAction() == MotionEvent.ACTION_DOWN) {
paint.setColor(Color.RED);
path = new Path();
path.moveTo(event.getX(), event.getY());
} else
if (event.getAction() == MotionEvent.ACTION_MOVE) {
int historySize = event.getHistorySize();
for (int i = 0; i < historySize; i++) {
path.lineTo(event.getHistoricalX(i),
event.getHistoricalY(i));
}
path.lineTo(event.getX(), event.getY());
canvas.drawPath(path, paint);
}
else {
return super.onTouchEvent(event);
}
invalidate();
return true;
}
解决方案
please try below working code i think it should be helpful to you
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
public class DrawableImageView extends View {
private Bitmap mBitmap;
private Bitmap pic;
private Canvas mCanvas;
private final Paint mPaint;
private int a = 255;
private int r = 255;
private int g = 255;
private int b = 255;
private float width = 4;
public DrawableImageView(Context c, Bitmap img) {
super(c);
pic = img;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setARGB(a,r,g,b);
Bitmap newBitmap = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.RGB_565);
Canvas newCanvas = new Canvas();
newCanvas.setBitmap(newBitmap);
if (img != null) {
newCanvas.drawBitmap(img, 0, 0, null);
}
mBitmap = newBitmap;
mCanvas = newCanvas;
mCanvas.setBitmap(mBitmap);
}
public DrawableImageView(Context c, Bitmap img, int alpha, int red, int green, int blue) {
this(c, img);
setColor(alpha, red, green, blue);
}
public DrawableImageView(Context c, Bitmap img, int alpha, int red, int green, int blue, float w) {
this(c, img, alpha, red, green, blue);
width = w;
}
public Bitmap getBitmap() {return mBitmap;}
public void setWidth(float w) {width = w;}
public void setColor(int alpha, int red, int green, int blue) {
a = alpha;
r = red;
g = green;
b = blue;
mPaint.setARGB(a,r,g,b);
}
public void Undo() {
mCanvas.drawBitmap(pic, 0, 0, null);
invalidate();
}
float scaleX;
float scaleY;
float scale;
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
scaleX = (float) w/mBitmap.getWidth();
scaleY = (float) h/mBitmap.getHeight();
scale = scaleX > scaleY ? scaleY : scaleX;
}
@Override protected void onDraw(Canvas canvas) {
if (mBitmap != null) {
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
canvas.drawBitmap(mBitmap, matrix, null);
//canvas.drawBitmap(mBitmap, 0,0, null);
}
}
float lastX;
float lastY;
@Override public boolean onTouchEvent(MotionEvent event) {
mPaint.setStrokeWidth(width/scale);
float curX = event.getX()/scale;
float curY = event.getY()/scale;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:{
mCanvas.drawCircle(curX, curY,width/2/scale, mPaint);
break;
}
case MotionEvent.ACTION_MOVE:{
mCanvas.drawLine(lastX, lastY, curX, curY, mPaint);
mCanvas.drawCircle(curX, curY,width/2/scale, mPaint); //fix for weird jaggies occur between line start and line stop
break;
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:{
mCanvas.drawLine(lastX, lastY, curX, curY, mPaint);
mCanvas.drawCircle(curX, curY,width/2/scale, mPaint);
break;
}
}
lastX = curX;
lastY = curY;
invalidate(); //invalidate only modified rect...
return true;
}
}
这篇关于怎么画的图像,图像保存到手机图库中的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文