Android實(shí)現(xiàn)畫(huà)畫(huà)板案例
本文實(shí)例為大家分享了Android實(shí)現(xiàn)畫(huà)畫(huà)板的具體代碼,供大家參考,具體內(nèi)容如下
① 準(zhǔn)備一個(gè)布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.it.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="修改顏色"
android:onClick="color"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="粗細(xì)+1"
android:onClick="size"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="保存圖片"
android:onClick="save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="@+id/iv_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
② 核心代碼
package com.example.it;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Environment;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();
private ImageView iv_show;
private Bitmap copyBm;
private float x;
private float y;
private Paint paint;
private int paintsize = 1;
private FileOutputStream fos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
iv_show = (ImageView) findViewById(R.id.iv_bg);
//創(chuàng)建一個(gè)空白的bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
copyBm = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//創(chuàng)建畫(huà)布
final Canvas canvas = new Canvas(copyBm);
//使用畫(huà)布繪制圖片
paint = new Paint();
canvas.drawBitmap(bitmap,new Matrix(), paint);
iv_show.setImageBitmap(copyBm);
//為畫(huà)筆在屏幕上移動(dòng)設(shè)置監(jiān)聽(tīng)事件
iv_show.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//獲取當(dāng)前事件的類(lèi)型
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
//如果是手指按下時(shí),記錄一下手指的位置
x = event.getX();
y = event.getY();
Log.e(TAG,x+"******"+y);
break;
case MotionEvent.ACTION_MOVE:
//如果手指正在移動(dòng)時(shí)
float x1 = event.getX();
float y1 = event.getY();
canvas.drawLine(x,y,x1,y1, paint);
x = x1;
y = y1;
Log.e(TAG,x1+"**********"+y1);
break;
case MotionEvent.ACTION_UP:
break;
}
iv_show.setImageBitmap(copyBm);
return true;
}
});
}
//修改顏色
public void color(View view) {
paint.setColor(Color.RED);
}
//修改線(xiàn)條粗細(xì)
public void size(View view) {
paintsize+=1;
paint.setStrokeWidth(paintsize);
}
//保存圖片
public void save(View view) {
//設(shè)置圖片保存的位置
File file = new File(Environment.getExternalStorageDirectory(), SystemClock.currentThreadTimeMillis()+".png");
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//設(shè)置參數(shù)保存的質(zhì)量
copyBm.compress(Bitmap.CompressFormat.PNG,100,fos);
//定義一個(gè)意圖,通過(guò)發(fā)出廣播告訴系統(tǒng)掃描指定的位置的文件
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
}
}
③ 權(quán)限信息
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android實(shí)現(xiàn)簡(jiǎn)單的畫(huà)畫(huà)板實(shí)例代碼
- Android實(shí)現(xiàn)畫(huà)板、寫(xiě)字板功能(附源碼下載)
- Android自定義SurfaceView實(shí)現(xiàn)畫(huà)板功能
- Android采用雙緩沖技術(shù)實(shí)現(xiàn)畫(huà)板
- Android畫(huà)板開(kāi)發(fā)之添加背景和保存畫(huà)板內(nèi)容為圖片
- Android畫(huà)板開(kāi)發(fā)之橡皮擦功能
- Android編程實(shí)現(xiàn)畫(huà)板功能的方法總結(jié)【附源碼下載】
- Android畫(huà)板開(kāi)發(fā)之撤銷(xiāo)反撤銷(xiāo)功能
- Android多媒體之畫(huà)畫(huà)板開(kāi)發(fā)案例分享
- Android自定義View實(shí)現(xiàn)簡(jiǎn)易畫(huà)板功能
相關(guān)文章
ijkPlayer播放器的全自動(dòng)編譯腳本及最終編譯包
這篇文章主要介紹了ijkPlayer播放器的全自動(dòng)編譯腳本及最終編譯包,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android中監(jiān)聽(tīng)系統(tǒng)網(wǎng)絡(luò)連接打開(kāi)或者關(guān)閉的實(shí)現(xiàn)代碼
本篇文章對(duì)Android中監(jiān)聽(tīng)系統(tǒng)網(wǎng)絡(luò)連接打開(kāi)或者關(guān)閉的實(shí)現(xiàn)用實(shí)例進(jìn)行了介紹。需要的朋友參考下2013-05-05
Android進(jìn)度條控件progressbar使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android進(jìn)度條控件progressbar的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
安卓(Android)中如何實(shí)現(xiàn)滑動(dòng)導(dǎo)航
導(dǎo)航是移動(dòng)應(yīng)用最重要的方面之一,對(duì)用戶(hù)體驗(yàn)是良好還是糟糕起著至關(guān)重要的作用。好的導(dǎo)航可以讓一款應(yīng)用更加易用并且讓用戶(hù)快速上手。相反,糟糕的應(yīng)用導(dǎo)航很容易讓人討厭,并遭到用戶(hù)的拋棄。2014-08-08
Android Studio 3.6運(yùn)行模擬器時(shí)Emulator警告問(wèn)題的解決方案
這篇文章主要介紹了Android Studio 3.6運(yùn)行模擬器時(shí)Emulator警告問(wèn)題的解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android中應(yīng)用界面主題Theme使用方法和頁(yè)面定時(shí)跳轉(zhuǎn)應(yīng)用
在A(yíng)ndroid SDK中內(nèi)置了下面的Theme,可以按標(biāo)題欄Title Bar和狀態(tài)欄Status Bar是否可見(jiàn)來(lái)分類(lèi),感興趣的朋友可以了解下哈2013-06-06
Android自定義控件實(shí)現(xiàn)icon+文字的多種效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)icon+文字的多種效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android開(kāi)發(fā)必知 九種對(duì)話(huà)框的實(shí)現(xiàn)方法
App中少不了與用戶(hù)交互的各種dialog,以此達(dá)到很好的用戶(hù)體驗(yàn),下面給大家介紹Android開(kāi)發(fā)必知 九種對(duì)話(huà)框的實(shí)現(xiàn)方法,有需要的朋友可以參考下2015-08-08

