AndroidStudio實現(xiàn)能在圖片上涂鴉程序
本文實例為大家分享了AndroidStudio實現(xiàn)能在圖片上涂鴉的具體代碼,供大家參考,具體內(nèi)容如下
一、內(nèi)容:設(shè)計一個能在圖片上涂鴉的程序
二、實現(xiàn)
1. 布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MainActivity" ? ? android:orientation="vertical"> ? ? ? <com.example.asus.test442.HandWrite ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="550dp" ? ? ? ? android:id="@+id/hw"/> ? ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="vertical"> ? ? ? ? <Button ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="clear" ? ? ? ? ? ? android:id="@+id/btn"/> ? ? ? </LinearLayout> ? </LinearLayout>
2. 主控文件MainActivity.java
package com.example.asus.test442; ? import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; ? public class MainActivity extends AppCompatActivity { ? ? private HandWrite handWrite = null; ? ? Button clear = null; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? handWrite = (HandWrite)findViewById(R.id.hw); //關(guān)聯(lián)view組件 ? ? ? ? clear = (Button)findViewById(R.id.btn); ? ? ? ? clear.setOnClickListener(new click()); ? ? } ? ? ? private class click implements View.OnClickListener { ? ? ? ? @Override ? ? ? ? public void onClick(View view) { ? ? ? ? ? ? handWrite.clear(); ? ? ? ? } ? ? } }
3. 記錄在屏幕上滑動的軌跡,實現(xiàn)在圖片上涂鴉的功能 HandWrite.java
package com.example.asus.test442; ? 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.util.AttributeSet; import android.view.MotionEvent; import android.view.View; ? public class HandWrite extends View { ? ? Paint paint = null; ?//定義畫筆 ? ? Bitmap origBit = null; ?//存放原始圖像 ? ? Bitmap new_1Bit = null; ? //存放從原始圖像復(fù)制的位圖圖像 ? ? Bitmap new_2Bit = null; ? ? ?//存放處理后的圖像 ? ? float startX = 0,startY = 0; ? //畫線的起點坐標 ? ? float clickX = 0, clickY = 0; ? //畫線的終點坐標 ? ? boolean isMove = true; ? //設(shè)置是否畫線的標記 ? ? boolean isClear = false; ? ?//設(shè)置是否清除涂鴉的標記 ? ? int color = Color.BLUE; ? ?//設(shè)置畫筆的顏色 ? ? float strokeWidth = 2.0f; ? ?//設(shè)置畫筆的寬度 ? ? public HandWrite(Context context, AttributeSet attrs) { ? ? ? ? super(context, attrs); ? ? ? ? // 從資源中獲取原始圖像 ? ? ? ? origBit = BitmapFactory.decodeResource(getResources(),R.drawable.p1).copy(Bitmap.Config.ARGB_8888,true); ? ? ? ? // 建立原始圖像的位圖 ? ? ? ? new_1Bit = Bitmap.createBitmap(origBit); ? ? } ? ? ? // 清除涂鴉 ? ? public void clear() { ? ? ? ? isClear = true; ? ? ? ? new_2Bit = Bitmap.createBitmap(origBit); ? ? ? ? invalidate(); ? ? } ? ? ? public void setSyle(float strokeWidth) { ? ? ? ? this.strokeWidth = strokeWidth; ? ? } ? ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? super.onDraw(canvas); ? ? ? ? canvas.drawBitmap(HandWriting(new_1Bit),0,0,null); ? ? } ? ? ? private Bitmap HandWriting(Bitmap newBit) { ?//記錄繪制圖形 ? ? ? ? Canvas canvas = null; ?// 定義畫布 ? ? ? ? if (isClear) { ?// 創(chuàng)建繪制新圖形的畫布 ? ? ? ? ? ? canvas = new Canvas(new_2Bit); ? ? ? ? } ? ? ? ? else { ? ? ? ? ? ? canvas = new Canvas(newBit); ?//創(chuàng)建繪制原圖形的畫布 ? ? ? ? } ? ? ? ? ? paint = new Paint(); ? ? ? ? paint.setStyle(Paint.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 new_2Bit; ?// 返回新繪制的圖像 ? ? ? ? } ? ? ? ? return newBit; ?// 若清屏,則返回原圖像 ? ? } ? ? ? // 定義觸摸屏事件 ? ? @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); ? ? } }
三、效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Recyclerview item中有EditText使用刷新遇到的坑
這篇文章主要介紹了詳解Recyclerview item中有EditText使用刷新遇到的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能
這篇文章主要為大家詳細介紹了Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06Android使用ViewDragHelper實現(xiàn)仿QQ6.0側(cè)滑界面(一)
這篇文章主要介紹了Android使用ViewDragHelper實現(xiàn)仿QQ6.0側(cè)滑界面(一)的相關(guān)資料,需要的朋友可以參考下2016-02-02Ubuntu 14.04下創(chuàng)建Genymotion安卓虛擬機的步驟詳解
Android 模擬器一直以速度奇慢無比著稱,基本慢到不可用。本文介紹我一直在用的 Genymotion,速度不亞于真機。而且功能齊全,使用簡單。下面這篇文章主要介紹了Ubuntu 14.04下創(chuàng)建Genymotion虛擬機的步驟,需要的朋友可以參考下。2017-03-03Android中new Notification創(chuàng)建實例的最佳方法
這篇文章主要介紹了Android中new Notification創(chuàng)建實例的最佳方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08Android中Bitmap、File與Uri之間的簡單記錄
這篇文章主要給大家介紹了關(guān)于Android中Bitmap、File與Uri之間的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02