Android實(shí)現(xiàn)手繪功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)手繪功能的具體代碼,供大家參考,具體內(nèi)容如下
布局文件如下
<?xml version='1.0' encoding='utf-8'?><RelativeLayout 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='com.example.administrator.main.DrawActivity'> <ImageView android: android:layout_width='1200px' android:layout_height='1500px' android:layout_alignParentLeft='true' android:layout_alignParentRight='true' android:layout_alignParentStart='true' /> <LinearLayout android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_alignParentBottom='true' android:layout_gravity='center_horizontal' android:orientation='horizontal'> </LinearLayout> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_alignParentBottom='true' android:layout_alignParentEnd='true' android:layout_marginEnd='79dp' android:text='重繪' /> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_alignBottom='@+id/linearLayout4' android:layout_marginStart='91dp' android:layout_toEndOf='@+id/linearLayout4' android:text='保存' /></RelativeLayout>
Activity代碼如下,其中線的顏色,寬度等屬性都可以修改。
package com.example.administrator.main; import android.content.Intent;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.net.Uri;import android.os.Environment;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast; import java.io.File;import java.io.FileOutputStream; public class DrawActivity extends AppCompatActivity { private ImageView iv; private Bitmap baseBitmap; private Button btn_resume; private Button btn_save; private Canvas canvas; private Paint paint; float radio; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_draw); radio = 10; iv = (ImageView) findViewById(R.id.iv); // 初始化一個(gè)畫(huà)筆,筆觸寬度為5,顏色為紅色 paint = new Paint(); paint.setStrokeWidth(radio); paint.setColor(Color.BLACK); iv = (ImageView) findViewById(R.id.iv); btn_resume = (Button) findViewById(R.id.btn_resume); btn_save = (Button) findViewById(R.id.btn_save); btn_resume.setOnClickListener(click); btn_save.setOnClickListener(click); iv.setOnTouchListener(touch); } private View.OnTouchListener touch = new View.OnTouchListener() { // 定義手指開(kāi)始觸摸的坐標(biāo) float startX; float startY; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { // 用戶按下動(dòng)作 case MotionEvent.ACTION_DOWN: // 第一次繪圖初始化內(nèi)存圖片,指定背景為白色 if (baseBitmap == null) { baseBitmap = Bitmap.createBitmap(iv.getWidth(),iv.getHeight(), Bitmap.Config.ARGB_8888); canvas = new Canvas(baseBitmap); canvas.drawColor(Color.WHITE); } // 記錄開(kāi)始觸摸的點(diǎn)的坐標(biāo) startX = event.getX(); startY = event.getY(); break; // 用戶手指在屏幕上移動(dòng)的動(dòng)作 case MotionEvent.ACTION_MOVE: // 記錄移動(dòng)位置的點(diǎn)的坐標(biāo) float stopX = event.getX(); float stopY = event.getY(); Thread t = new Thread(new Runnable() { @Override public void run() { radio += 0.1;try {Thread.sleep(1000); } catch (InterruptedException e) {e.printStackTrace(); } } }); t.start(); paint.setStrokeWidth(radio); //根據(jù)兩點(diǎn)坐標(biāo),繪制連線 canvas.drawLine(startX, startY, stopX, stopY, paint); // 更新開(kāi)始點(diǎn)的位置 startX = event.getX(); startY = event.getY(); // 把圖片展示到ImageView中 iv.setImageBitmap(baseBitmap); break; case MotionEvent.ACTION_UP: radio = 5; break; default: break; } return true; } }; private View.OnClickListener click = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_save: saveBitmap(); break; case R.id.btn_resume: resumeCanvas(); break; default: break; } } }; /** * 保存圖片到SD卡上 */ protected void saveBitmap() { try { // 保存圖片到SD卡上 String fileName = '/sdcard/'+System.currentTimeMillis() + '.png'; File file = new File(fileName); FileOutputStream stream = new FileOutputStream(file); baseBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); Toast.makeText(DrawActivity.this, '保存圖片成功', Toast.LENGTH_SHORT).show(); // Android設(shè)備Gallery應(yīng)用只會(huì)在啟動(dòng)的時(shí)候掃描系統(tǒng)文件夾 // 這里模擬一個(gè)媒體裝載的廣播,用于使保存的圖片可以在Gallery中查看 Intent intent = new Intent(); intent.setAction(Intent.ACTION_MEDIA_MOUNTED); intent.setData(Uri.fromFile(Environment .getExternalStorageDirectory())); sendBroadcast(intent); } catch (Exception e) { Toast.makeText(DrawActivity.this, '保存圖片失敗', Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } // 手動(dòng)清除畫(huà)板的繪圖,重新創(chuàng)建一個(gè)畫(huà)板 protected void resumeCanvas() { if (baseBitmap != null) { baseBitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.ARGB_8888); canvas = new Canvas(baseBitmap); canvas.drawColor(Color.WHITE); iv.setImageBitmap(baseBitmap); Toast.makeText(DrawActivity.this, '清除畫(huà)板成功,可以重新開(kāi)始繪圖', Toast.LENGTH_SHORT).show(); } }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ajax post下載flask文件流以及中文文件名問(wèn)題2. 如何通過(guò)vscode運(yùn)行調(diào)試javascript代碼3. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能4. WML語(yǔ)言的基本情況5. 利用CSS3新特性創(chuàng)建透明邊框三角6. 使用Docker的NFS-Ganesha鏡像搭建nfs服務(wù)器的詳細(xì)過(guò)程7. IntelliJ IDEA導(dǎo)入jar包的方法8. 利用CSS制作3D動(dòng)畫(huà)9. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……10. ASP.NET泛型三之使用協(xié)變和逆變實(shí)現(xiàn)類(lèi)型轉(zhuǎn)換
