android 實(shí)現(xiàn)在照片上繪制涂鴉的方法
這個(gè)應(yīng)該是簡易版的美圖秀秀(小伙伴們吐槽:你這也叫簡易版的??我們看著怎么不像啊……)。好吧,只是在圖片上繪制涂鴉,然后保存。
一、選擇圖片
這個(gè)道長有必要說一下,在繪制涂鴉時(shí),筆畫會(huì)根據(jù)設(shè)置ImageView的大小和屏幕的尺寸(不是像素)產(chǎn)生誤差。這個(gè)道長暫時(shí)還沒有找到解決方法,只是規(guī)避了一下。
把ImageView設(shè)置為全屏,布局文件代碼如下
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:orientation="vertical"> <ImageView android:id="@+id/iv_draw_pic" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_margin="10dp" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn_choose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:text="選擇照片" /> <Button android:id="@+id/btn_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_margin="5dp" android:layout_weight="1" android:text="保存照片" /> <Button android:id="@+id/btn_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_margin="5dp" android:layout_weight="1" android:text="擦除筆跡" /> </RelativeLayout> </FrameLayout>
根據(jù)機(jī)型設(shè)置縮放比例
switch (model){ case "MI 4LTE": scale = 1.1f; break; case "HUAWEI NXT-AL10": scale = 1.5f; break; }
效果圖如下
二、繪制涂鴉
實(shí)現(xiàn)代碼如下:
@Override public boolean onTouch(View view, MotionEvent motionEvent) { int action = motionEvent.getAction(); switch (action) { case MotionEvent.ACTION_CANCEL: break; case MotionEvent.ACTION_DOWN: downX = motionEvent.getX() * scale; downY = motionEvent.getY() * scale; break; case MotionEvent.ACTION_UP: upX = motionEvent.getX() * scale; upY = motionEvent.getY() * scale; canvas.drawLine(downX, downY, upX, upY, paint); iv_drawpicture.invalidate(); break; case MotionEvent.ACTION_MOVE: upX = motionEvent.getX() * scale; upY = motionEvent.getY() * scale; canvas.drawLine(downX, downY, upX, upY, paint); iv_drawpicture.invalidate(); downX = upX; downY = upY; break; default: break; } return true; }
效果圖如下:
三、保存繪制涂鴉后的圖片
實(shí)現(xiàn)代碼如下:
try { Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues()); OutputStream os = getContentResolver().openOutputStream(imageUri); //compress方法將圖片轉(zhuǎn)換成JPG或者PNG格式 drawBitmap.compress(Bitmap.CompressFormat.JPEG, 90, os); Toast.makeText(this, "Saved:" + imageUri.toString(), Toast.LENGTH_LONG).show(); } catch (FileNotFoundException e) { e.printStackTrace(); }
四、擦除涂鴉筆跡
實(shí)現(xiàn)代碼如下:
drawBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig()); canvas = createCanvas(drawBitmap); paint = createPaint(); canvas.drawBitmap(bmp, 0, 0, paint); iv_drawpicture.setImageBitmap(drawBitmap); iv_drawpicture.setOnTouchListener(this);
在照片上繪制涂鴉暫時(shí)就到這里,功能比較單一。
以上這篇android 實(shí)現(xiàn)在照片上繪制涂鴉的方法就是小編分享給大家的全部內(nèi)容了,希望這篇文章能夠?yàn)樾』锇閭兲峁┮恍椭?,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)之關(guān)于項(xiàng)目
本文是此系列文章的第二篇,給大家介紹的是項(xiàng)目相關(guān)的內(nèi)容,非常的細(xì)致全面,有需要的小伙伴可以參考下2016-02-02Android自定義View onDraw()方法會(huì)調(diào)用兩次的問題解決
這篇文章主要介紹了Android自定義View onDraw()方法會(huì)調(diào)用兩次的問題解決,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01Android實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的全過程
在Android開發(fā)中,經(jīng)常需要設(shè)置控件的背景顏色或者圖片的src顏色,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的相關(guān)資料,本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09如何利用Flutter實(shí)現(xiàn)酷狗流暢Tabbar效果
這篇文章主要給大家介紹了關(guān)于如何利用Flutter實(shí)現(xiàn)酷狗流暢Tabbar效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02Android EditText限制輸入字?jǐn)?shù)的方法
這篇文章主要介紹了Android EditText限制輸入字?jǐn)?shù)的方法,涉及Android針對EditText文本與字符串操作相關(guān)技巧,需要的朋友可以參考下2016-01-01