Android編程實(shí)現(xiàn)在Bitmap上涂鴉效果
本文實(shí)例講述了Android編程實(shí)現(xiàn)在Bitmap上涂鴉效果。分享給大家供大家參考,具體如下:
布局文件:
<?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" > <LinearLayout android:id="@+id/handwriteview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android:id="@+id/clear" android:layout_width="200dp" android:layout_height="wrap_content" android:text="清屏" /> </LinearLayout> </LinearLayout>
重寫的View文件:
public class HandWrite 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 HandWrite(Context context,Bitmap b)
{
super(context);
originalBitmap = Bitmap.createBitmap(b).copy(Bitmap.Config.ARGB_8888, true);
new1Bitmap = Bitmap.createBitmap(originalBitmap);
}
public void clear(){
isClear = true;
new2Bitmap = Bitmap.createBitmap(originalBitmap);
invalidate();
}
public void setstyle(float strokeWidth){
this.strokeWidth = strokeWidth;
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawBitmap(HandWriting(new1Bitmap), 0, 0,null);
}
public Bitmap HandWriting(Bitmap originalBitmap)
{
Canvas canvas = null;
if(isClear){
canvas = new Canvas(new2Bitmap);
}
else{
canvas = new Canvas(originalBitmap);
}
paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setAntiAlias(true);
paint.setColor(color);
paint.setStrokeWidth(strokeWidth);
if(isMove){
canvas.drawLine(startX, startY, clickX, clickY, paint);
}
startX = clickX;
startY = clickY;
if(isClear){
return new2Bitmap;
}
return originalBitmap;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
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);
}
}
Activity文件:
public class HandWritingActivity extends Activity
{
/** Called when the activity is first created. */
private LinearLayout handWrite = null;
private Button clear = null;
int requestWidth=116;
int requestHeight=173;
int inSampleSize;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hand_writing);
handWrite = (LinearLayout)findViewById(R.id.handwriteview);
clear = (Button)findViewById(R.id.clear);
clear.setOnClickListener(new clearListener());
}
private class clearListener implements OnClickListener{
public void onClick(View v)
{
// handWrite.clear();
BitmapFactory.Options opts = new Options();
opts.inJustDecodeBounds = true;// 讓 bimapfactory假的解析這個(gè)位圖,只獲取位圖的邊框信息
BitmapFactory.decodeResource(getResources(), R.drawable.cool, opts);
if (opts.outHeight > requestHeight || opts.outWidth > requestWidth) {
if (opts.outWidth > opts.outHeight) {
inSampleSize = Math.round((float) opts.outHeight
/ (float) requestHeight);
} else {
inSampleSize = Math.round((float) opts.outWidth
/ (float) requestWidth);
}
}
System.out.println("寬度:" + opts.outWidth);
System.out.println("高度:" + opts.outHeight);
opts.inSampleSize = inSampleSize;
System.out.println(inSampleSize);
opts.inJustDecodeBounds = false;// 由于已經(jīng)得到了縮放比例 ,讓位圖工廠真正的解析這個(gè)位圖
// 由于前面 我們已經(jīng)解析了這個(gè)輸入流, 需要重新初始化這個(gè)輸入流
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.cool, opts);
HandWrite hw = new HandWrite(HandWritingActivity.this, b);
System.out.println(b.getWidth());
handWrite.addView(hw);
}
}
}
整合的一個(gè)涂鴉工具類:
/**
* 使用方法:
* 1. 創(chuàng)建TuYaView類實(shí)例
* 2. 調(diào)用drawTuya方法
* 3. 參數(shù)1:context
* 4. 參數(shù)2:圖像的byte[]字節(jié)數(shù)組
* 5. ImageView實(shí)例
* 6. 畫筆定義
* **/
import com.ziipin.lhdc.utils.ToastUtil;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.BitmapFactory.Options;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class TuYaView {
// 原始圖片
private Bitmap mOrignBitmap;
private Bitmap mEditBitmap;
private int inSampleSize;
private int requestWidth = 500;
private int requestHeight = 700;
/** 編輯圖片的畫布 */
private Canvas mCanvas;
private ImageView image;
private Paint mPaint;
public Bitmap drawTuya(Context context, byte[] _data, ImageView image,
Paint mPaint) {
this.image = image;
this.mPaint = mPaint;
mOrignBitmap = BitmapFactory.decodeByteArray(_data, 0, _data.length);
return showEditBitmap(context, _data, image);
}
/**
* 顯示編輯的圖片
*/
private Bitmap showEditBitmap(Context context, byte[] _data, ImageView image) {
mOrignBitmap = getScaleBitmap(_data, image);
if (mOrignBitmap == null) {
ToastUtil.show(context, "編輯出錯(cuò)");
}
mEditBitmap = mOrignBitmap.copy(mOrignBitmap.getConfig(), true);
mCanvas = new Canvas(mEditBitmap);
mCanvas.drawBitmap(mOrignBitmap, new Matrix(), new Paint());
image.setImageBitmap(mEditBitmap);
image.setOnTouchListener(mTouchListener);
return mEditBitmap;
}
/**
* 獲取結(jié)果縮放放后的圖片
*
* @return
*/
private Bitmap getScaleBitmap(byte[] _data, ImageView image) {
BitmapFactory.Options opts = new Options();
opts.inJustDecodeBounds = true;// 讓 bimapfactory假的解析這個(gè)位圖,只獲取位圖的邊框信息
BitmapFactory.decodeByteArray(_data, 0, _data.length, opts);
if (opts.outHeight > requestHeight || opts.outWidth > requestWidth) {
if (opts.outWidth > opts.outHeight) {
inSampleSize = Math.round((float) opts.outHeight
/ (float) requestHeight);
} else {
inSampleSize = Math.round((float) opts.outWidth
/ (float) requestWidth);
}
}
opts.inSampleSize = inSampleSize;
opts.inJustDecodeBounds = false;// 由于已經(jīng)得到了縮放比例 ,讓位圖工廠真正的解析這個(gè)位圖
// 由于前面 我們已經(jīng)解析了這個(gè)輸入流, 需要重新初始化這個(gè)輸入流
Bitmap bmp = BitmapFactory
.decodeByteArray(_data, 0, _data.length, opts);
return bmp;
}
// touch事件
private OnTouchListener mTouchListener = new OnTouchListener() {
int startx = 0;
int starty = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 手指第一次觸摸屏幕
startx = (int) event.getX();
starty = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE: // 手指在imageview上中移動(dòng)
int x = (int) event.getX();
int y = (int) event.getY();
mCanvas.drawLine(startx, starty, x, y, mPaint);
startx = (int) event.getX();
starty = (int) event.getY();
image.invalidate();
break;
}
return true;
}
};
}
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能
本篇文章主要介紹了android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
Android技巧一之啟動(dòng)屏+新功能左右導(dǎo)航邏輯
這篇文章主要介紹了Android技巧一之啟動(dòng)屏+新功能左右導(dǎo)航邏輯的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android學(xué)習(xí)教程之滑動(dòng)布局(14)
這篇文章主要為大家詳細(xì)介紹了Android學(xué)習(xí)教程之滑動(dòng)布局使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Kotlin如何安全訪問lateinit變量的實(shí)現(xiàn)
這篇文章主要介紹了Kotlin如何安全訪問lateinit變量的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Android自定義控件實(shí)現(xiàn)圓形進(jìn)度CircleProgressBar
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)圓形進(jìn)度CircleProgressBar,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Android?應(yīng)用程序的啟動(dòng)流程示例詳解
這篇文章主要為大家介紹了Android?應(yīng)用程序的啟動(dòng)流程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android使用模板生成支持手機(jī)直接查看的Word文檔
這篇文章主要為大家詳細(xì)介紹了Android 使用模板生成Word文檔,支持手機(jī)直接查看word,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
詳談OnTouchListener與OnGestureListener的區(qū)別
下面小編就為大家?guī)硪黄斦凮nTouchListener與OnGestureListener的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Android實(shí)現(xiàn)TextView兩端對(duì)齊的方法
這篇文章主要介紹了Android實(shí)現(xiàn)TextView兩端對(duì)齊的方法,需要的朋友可以參考下2016-01-01

