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; ?//定義畫筆
? ? Bitmap origBit = null; ?//存放原始圖像
? ? Bitmap new_1Bit = null; ? //存放從原始圖像復(fù)制的位圖圖像
? ? Bitmap new_2Bit = null; ? ? ?//存放處理后的圖像
? ? float startX = 0,startY = 0; ? //畫線的起點(diǎn)坐標(biāo)
? ? float clickX = 0, clickY = 0; ? //畫線的終點(diǎn)坐標(biāo)
? ? boolean isMove = true; ? //設(shè)置是否畫線的標(biāo)記
? ? boolean isClear = false; ? ?//設(shè)置是否清除涂鴉的標(biāo)記
? ? 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(); ?// 獲取觸摸坐標(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)文章
android studio 4.0 新建類沒有修飾符的方法
這篇文章主要介紹了android studio 4.0 新建類沒有修飾符的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Android實(shí)現(xiàn)去哪兒攜程地址互換效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)去哪兒攜程地址互換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android一步步帶你在RecyclerView上面實(shí)現(xiàn)"拖放"和"滑動(dòng)刪除"功能
這篇文章主要介紹了Android一步步帶你在RecyclerView上面實(shí)現(xiàn)"拖放"和"滑動(dòng)刪除"功能,需非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-03-03
android 開發(fā)中使用okhttp上傳文件到服務(wù)器
在開發(fā)android手機(jī)客戶端,常常會(huì)需要上傳文件到服務(wù)器,使用okhttp會(huì)是一個(gè)很好的選擇,它使用很簡(jiǎn)單,而且運(yùn)行效率也很高,下面小編給大家?guī)?lái)了android 開發(fā)中使用okhttp上傳文件到服務(wù)器功能,一起看看吧2018-01-01
android模擬器開發(fā)和測(cè)試nfc應(yīng)用實(shí)例詳解
本文介紹android模擬器開發(fā)nfc應(yīng)用詳解,大家參考使用吧2013-12-12
淺析Android手機(jī)衛(wèi)士之抖動(dòng)輸入框和手機(jī)震動(dòng)
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士之輸入框抖動(dòng)和手機(jī)震動(dòng)的相關(guān)資料,需要的朋友可以參考下2016-04-04

