日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android涂鸦板保存功能,android实现涂鸦,保存涂鸦后的图片,清屏

發布時間:2025/3/15 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android涂鸦板保存功能,android实现涂鸦,保存涂鸦后的图片,清屏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

自定義view的類,代碼如下:

[html]

package com.xy.tuya;

import android.annotation.SuppressLint;

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.Paint.Style;

import android.os.Handler;

import android.os.Message;

import android.util.AttributeSet;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

public class MyView extends View {

private Paint paint = null;

private Bitmap originalBitmap = null;

private Bitmap new1Bitmap = null;

private Bitmap new2Bitmap = null;

private float clickX = 0, clickY = 0;

private float startX = 0, startY = 0;

private boolean isMove = true;

private boolean isClear = false;

private int color = Color.GREEN;

private float strokeWidth = 2.0f;

public MyView(Context context, AttributeSet attrs) {

super(context, attrs);

originalBitmap = BitmapFactory.decodeResource(getResources(),

R.drawable.a1).copy(Bitmap.Config.ARGB_8888, true);

new1Bitmap = Bitmap.createBitmap(originalBitmap);

setDrawingCacheEnabled(true);

Log.i("RG", "new1Bitmap--->>>" + new1Bitmap);

}

public void clear() {

isClear = true;

new2Bitmap = Bitmap.createBitmap(originalBitmap);

invalidate();

}

Bitmap saveImage = null;

public Bitmap saveImage() {

if (saveImage == null) {

return null;

}

return saveImage;

}

public void setImge() {

saveImage = null;

}

public void setstyle(float strokeWidth) {

this.strokeWidth = strokeWidth;

}

Handler handler1;

@SuppressLint("DrawAllocation")

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawBitmap(HandWriting(new1Bitmap), 0, 0, null);

handler1 = new Handler() {

@Override

public void handleMessage(Message msg) {

Log.i("RG", "--------------------");

int what = msg.what;

if (what == 2) {

saveImage = Bitmap.createBitmap(HandWriting(new1Bitmap));

Message msg1 = new Message();

msg1 = Message.obtain(MainActivity.hh, 3);

MainActivity.hh.sendMessage(msg1);

}

super.handleMessage(msg);

}

};

}

@SuppressLint("HandlerLeak")

Handler handler;

public Bitmap HandWriting(Bitmap originalBitmap) {

handler = new Handler() {

@Override

public void handleMessage(Message msg) {

int what = msg.what;

if (what == 1) {

startX = clickX;

startY = clickY;

}

super.handleMessage(msg);

}

};

Canvas canvas = null;

if (isClear) {

canvas = new Canvas(new2Bitmap);

Log.i("RG", "canvas ");

} else {

canvas = new Canvas(originalBitmap);

}

paint = new Paint();

paint.setStyle(Style.STROKE);

paint.setAntiAlias(true);

paint.setColor(color);

paint.setStrokeWidth(strokeWidth);

Log.i("RG", "startX-->>>>" + startX);

Log.i("RG", "startY-->>>>" + startY);

if (isMove) {

canvas.drawLine(startX, startY, clickX, clickY, paint);

}

if (isClear) {

return new2Bitmap;

}

return originalBitmap;

}

@Override

public boolean onTouchEvent(MotionEvent event) {

Message msg = new Message();

msg = Message.obtain(handler, 1);

handler.sendMessage(msg);

clickX = event.getX();

clickY = event.getY();

if (event.getAction() == MotionEvent.ACTION_DOWN) {

isMove = false;

invalidate();

return true;

} else if (event.getAction() == MotionEvent.ACTION_MOVE) {

isMove = true;

invalidate();

return true;

}

return super.onTouchEvent(event);

}

}

mainactiviry類,代碼如下:

[html]

package com.xy.tuya;

import android.opengl.Visibility;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.drawable.Drawable;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

/** Called when the activity is first created. */

private MyView handWrite = null;

private Button clear = null;

private Button save = null;

private ImageView saveImage = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

handWrite = (MyView) findViewById(R.id.handwriteview);

save = (Button) findViewById(R.id.save);

saveImage = (ImageView) findViewById(R.id.savep_w_picpath);

clear = (Button) findViewById(R.id.clear);

clear.setOnClickListener(new clearListener());

save.setOnClickListener(new saveListener());

handerl();

}

private class clearListener implements OnClickListener {

public void onClick(View v) {

handWrite.clear();

}

}

static Handler hh;

public void handerl() {

hh = new Handler() {

@Override

public void handleMessage(Message msg) {

int what = msg.what;

if (what == 3) {

saveImage.setVisibility(View.VISIBLE);

saveImage.setImageBitmap(handWrite.saveImage());

handWrite.setImge();

}

super.handleMessage(msg);

}

};

}

private class saveListener implements OnClickListener {

@Override

public void onClick(View v) {

Message msg = new Message();

msg = Message.obtain(handWrite.handler1, 2);

handWrite.handler1.sendMessage(msg);

if (handWrite.saveImage() != null) {

Log.i("RG", "111111111111111111111");

} else {

saveImage.setVisibility(View.GONE);

}

}

}

}

xml布局代碼如下:

[html]

< ?xml version="1.0" encoding="utf-8"?>

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content" >

android:id="@+id/handwriteview"

android:layout_width="fill_parent"

android:layout_height="300dp" />

android:layout_width="wrap_content"

android:layout_height="40dip"

android:orientation="horizontal" >

android:id="@+id/clear"

android:layout_width="50dp"

android:layout_height="wrap_content"

android:text="清屏" />

android:id="@+id/save"

android:layout_width="50dp"

android:layout_height="wrap_content"

android:text="保存" />

android:id="@+id/savep_w_picpath"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

< /LinearLayout>

下過圖如下:

總結

以上是生活随笔為你收集整理的android涂鸦板保存功能,android实现涂鸦,保存涂鸦后的图片,清屏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。