欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AndroidStudio實現(xiàn)能在圖片上涂鴉程序

 更新時間:2022年05月17日 14:33:31   作者:Allison-L  
這篇文章主要為大家詳細介紹了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)文章

最新評論