AndroidStudio:设计一个能在图片上涂鸦的程序
生活随笔
收集整理的這篇文章主要介紹了
AndroidStudio:设计一个能在图片上涂鸦的程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一內(nèi)容:設(shè)計一個能在圖片上涂鴉的程序
二實現(xiàn)
①布局文件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.HandWriteandroid:layout_width="match_parent"android:layout_height="550dp"android:id="@+id/hw"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="clear"android:id="@+id/btn"/></LinearLayout></LinearLayout>②主控文件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;@Overrideprotected 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 {@Overridepublic 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;}@Overrideprotected 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; // 若清屏,則返回原圖像}// 定義觸摸屏事件@Overridepublic 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);} }三效果
總結(jié)
以上是生活随笔為你收集整理的AndroidStudio:设计一个能在图片上涂鸦的程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PS教程第二课:PS安装
- 下一篇: Python 修改图片的时候抗锯齿