AndroidStudio實(shí)現(xiàn)能在圖片上涂鴉程序
本文實(shí)例為大家分享了AndroidStudio實(shí)現(xiàn)能在圖片上涂鴉的具體代碼,供大家參考,具體內(nèi)容如下
一、內(nèi)容:設(shè)計(jì)一個(gè)能在圖片上涂鴉的程序
二、實(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. 記錄在屏幕上滑動(dòng)的軌跡,實(shí)現(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; ?//定義畫(huà)筆
? ? Bitmap origBit = null; ?//存放原始圖像
? ? Bitmap new_1Bit = null; ? //存放從原始圖像復(fù)制的位圖圖像
? ? Bitmap new_2Bit = null; ? ? ?//存放處理后的圖像
? ? float startX = 0,startY = 0; ? //畫(huà)線的起點(diǎn)坐標(biāo)
? ? float clickX = 0, clickY = 0; ? //畫(huà)線的終點(diǎn)坐標(biāo)
? ? boolean isMove = true; ? //設(shè)置是否畫(huà)線的標(biāo)記
? ? boolean isClear = false; ? ?//設(shè)置是否清除涂鴉的標(biāo)記
? ? int color = Color.BLUE; ? ?//設(shè)置畫(huà)筆的顏色
? ? float strokeWidth = 2.0f; ? ?//設(shè)置畫(huà)筆的寬度
? ? 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; ?// 定義畫(huà)布
? ? ? ? if (isClear) { ?// 創(chuàng)建繪制新圖形的畫(huà)布
? ? ? ? ? ? canvas = new Canvas(new_2Bit);
? ? ? ? }
? ? ? ? else {
? ? ? ? ? ? canvas = new Canvas(newBit); ?//創(chuàng)建繪制原圖形的畫(huà)布
? ? ? ? }
?
? ? ? ? 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); ?// 在畫(huà)布上畫(huà)線條
? ? ? ? }
? ? ? ? startX = clickX;
? ? ? ? startY = clickY;
?
? ? ? ? if (isClear){
? ? ? ? ? ? return new_2Bit; ?// 返回新繪制的圖像
? ? ? ? }
? ? ? ? return newBit; ?// 若清屏,則返回原圖像
? ? }
?
? ? // 定義觸摸屏事件
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? clickX = event.getX(); ?// 獲取觸摸坐標(biāo)位置
? ? ? ? clickY = event.getY();
? ? ? ? if (event.getAction() == MotionEvent.ACTION_DOWN) { ?// 按下屏幕時(shí)無(wú)繪圖
? ? ? ? ? ? isMove = false;
? ? ? ? ? ? invalidate();
? ? ? ? ? ? return true;
? ? ? ? } else if (event.getAction() == MotionEvent.ACTION_MOVE) { ?// 記錄在屏幕上劃動(dòng)的軌跡
? ? ? ? ? ? isMove = true;
? ? ? ? ? ? invalidate();
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? return super.onTouchEvent(event);
? ? }
}三、效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Recyclerview item中有EditText使用刷新遇到的坑
這篇文章主要介紹了詳解Recyclerview item中有EditText使用刷新遇到的坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android仿ViVO X6 極速閃充動(dòng)畫(huà)效果
這篇文章主要介紹了Android仿ViVO X6 極速閃充動(dòng)畫(huà)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)動(dòng)態(tài)自動(dòng)匹配輸入內(nèi)容功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android使用ViewDragHelper實(shí)現(xiàn)仿QQ6.0側(cè)滑界面(一)
這篇文章主要介紹了Android使用ViewDragHelper實(shí)現(xiàn)仿QQ6.0側(cè)滑界面(一)的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android開(kāi)發(fā)中聽(tīng)筒無(wú)法播放音樂(lè)的解決方法
這篇文章主要介紹了Android開(kāi)發(fā)中聽(tīng)筒無(wú)法播放音樂(lè)的解決方法,涉及Android權(quán)限控制中的相關(guān)屬性設(shè)置技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
Ubuntu 14.04下創(chuàng)建Genymotion安卓虛擬機(jī)的步驟詳解
Android 模擬器一直以速度奇慢無(wú)比著稱,基本慢到不可用。本文介紹我一直在用的 Genymotion,速度不亞于真機(jī)。而且功能齊全,使用簡(jiǎn)單。下面這篇文章主要介紹了Ubuntu 14.04下創(chuàng)建Genymotion虛擬機(jī)的步驟,需要的朋友可以參考下。2017-03-03
Android中new Notification創(chuàng)建實(shí)例的最佳方法
這篇文章主要介紹了Android中new Notification創(chuàng)建實(shí)例的最佳方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
Android中Bitmap、File與Uri之間的簡(jiǎn)單記錄
這篇文章主要給大家介紹了關(guān)于Android中Bitmap、File與Uri之間的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02

