Android實現畫板功能(二)
本文實例為大家分享了Android實現畫板功能的具體代碼,講解使用imageView,bitmap的方式實現畫板功能,供大家參考,具體內容如下
前言在上一篇Android實現畫板功能(一)文章中我介紹過用自定義view的方式實現畫板功能,在這篇文章中繼續講解使用imageView,bitmap的方式實現畫板功能。也是非常簡單,初始化canvas,paint,創建和imageView一樣大的bitmap,當手指點擊屏幕時記錄下初始位置,手指移動時傳遞當前位置,調用canvas的draw Line方法就可以實現畫圖的效果了。如果想要保存畫出來的圖片,把bitmap保存下來即可。
效果圖既然開發出了畫板,那就隨便畫一點吧(畫圖我已經盡力了)。
布局文件
<?xml version='1.0' encoding='utf-8'?><RelativeLayout 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' tools:context='.MainActivity'> <RelativeLayoutandroid:layout_width='match_parent'android:layout_height='55dp'android:background='@color/teal_200'> <TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='我的畫板' android:layout_marginStart='10dp' android:layout_centerVertical='true' android:textColor='@android:color/white' android:textSize='16sp'/> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='清除' android:layout_alignParentEnd='true' android:layout_marginEnd='10dp' android:layout_centerVertical='true' android:textColor='@android:color/white' android:textSize='16sp'/> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='擦除' android:layout_toStartOf='@id/text_clear' android:layout_marginEnd='10dp' android:layout_centerVertical='true' android:textColor='@android:color/white' android:textSize='16sp'/> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='藍色' android:layout_toStartOf='@id/text_eraser' android:layout_marginEnd='10dp' android:layout_centerVertical='true' android:textColor='@android:color/white' android:textSize='16sp'/> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='紅色' android:layout_toStartOf='@id/text_blue' android:layout_marginEnd='10dp' android:layout_centerVertical='true' android:textColor='@android:color/white' android:textSize='16sp'/> </RelativeLayout> <ImageViewandroid: android:layout_marginTop='55dp'android:layout_width='match_parent'android:layout_height='match_parent'> </ImageView></RelativeLayout>
DrawLineView
import android.annotation.SuppressLint import android.graphics.* import android.view.MotionEvent import android.widget.ImageView class DrawLineView (view: ImageView){ private var defaultPaint: Paintprivate var canvas: Canvasprivate var bitmap: Bitmapprivate var imageView:ImageViewprivate var startX = 0fprivate var startY = 0f init { imageView = view bitmap = Bitmap.createBitmap(imageView.width, imageView.height, Bitmap.Config.ARGB_8888) canvas = Canvas(bitmap) canvas.drawColor(Color.WHITE) defaultPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG) defaultPaint.style = Paint.Style.STROKE defaultPaint.strokeWidth = 5f defaultPaint.color = Color.RED canvas.drawBitmap(bitmap, Matrix(), defaultPaint) imageView.setImageBitmap(bitmap) eventHandler()} @SuppressLint('ClickableViewAccessibility')private fun eventHandler() { imageView.setOnTouchListener { _, event ->when (event.action) { MotionEvent.ACTION_DOWN -> {startX = event.xstartY = event.y } MotionEvent.ACTION_MOVE -> {val endX = event.xval endY = event.ycanvas.drawLine(startX, startY, endX, endY, defaultPaint)startX = event.xstartY = event.yimageView.setImageBitmap(bitmap) } MotionEvent.ACTION_UP -> { }}true }} fun clear(){ bitmap.eraseColor(Color.WHITE) imageView.setImageBitmap(bitmap)} fun blue(){ defaultPaint.color = Color.BLUE} fun red(){ defaultPaint.color = Color.RED} fun eraser(){ defaultPaint.color = Color.WHITE}}
這是我自己封裝的DrawLineView類,在init方法中初始化bitmap和canvas,傳進來的bitmap的寬高就是imageView的寬高。然后是初始化canvas,paint。接下來是監聽imageView的觸摸事件。
當手指點擊屏幕時記錄下xy軸的位置,手指移動時只需要調用canvas的drawLine方法就可以畫出一條線了。給drawLine方法傳遞初始位置,現在的位置和一個paint參數,我們可以控制畫筆的粗細程度,顏色等。這里有朋友們可能會想,我調用的是canvas的drawLine方法,這和bitmap有什么關系呢?其實我們畫的就是一個個像素點組成的位圖,用bitmap來存儲這些像素點。drawLine方法的任務就是把這些像素點記錄在bitmap上面。最后就是把bitmap傳給imageView顯示出來。
MainActivity
package com.example.drawline import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) image.post {val lineView = DrawLineView(image) text_clear.setOnClickListener { lineView.clear() } text_blue.setOnClickListener { lineView.blue() } text_red.setOnClickListener { lineView.red() } text_eraser.setOnClickListener { lineView.eraser() } }}}
因為創建bitmap時我們傳遞的了imageView的寬高,如果image View的寬高還沒測量完就傳到bitmap里面,這時候傳遞的可能是負數,這導致無法創建bitmap。所以這里先等到image View完全繪制完畢,再傳遞它的寬高即可。在網上看到別人用了一張背景圖,然后傳給bitmap的是這個背景圖的大小,這也是解決辦法之一。大家可以按照自己的需求選擇合理的方法就可以。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: