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

Android實(shí)現(xiàn)畫板功能(二)

 更新時(shí)間:2021年09月10日 15:29:35   作者:吐?tīng)柡榻瑿oding  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)畫板功能的第二篇,使用imageView,bitmap方式實(shí)現(xiàn)畫板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)畫板功能的具體代碼,講解使用imageView,bitmap的方式實(shí)現(xiàn)畫板功能,供大家參考,具體內(nèi)容如下

前言

在上一篇Android實(shí)現(xiàn)畫板功能(一)文章中我介紹過(guò)用自定義view的方式實(shí)現(xiàn)畫板功能,在這篇文章中繼續(xù)講解使用imageView,bitmap的方式實(shí)現(xiàn)畫板功能。也是非常簡(jiǎn)單,初始化canvas,paint,創(chuàng)建和imageView一樣大的bitmap,當(dāng)手指點(diǎn)擊屏幕時(shí)記錄下初始位置,手指移動(dòng)時(shí)傳遞當(dāng)前位置,調(diào)用canvas的draw Line方法就可以實(shí)現(xiàn)畫圖的效果了。如果想要保存畫出來(lái)的圖片,把bitmap保存下來(lái)即可。

效果圖

既然開發(fā)出了畫板,那就隨便畫一點(diǎn)吧(畫圖我已經(jīng)盡力了)。

布局文件

<?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">
 
    <RelativeLayout
        android: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:id="@+id/text_clear"
            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:id="@+id/text_eraser"
            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:id="@+id/text_blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="藍(lán)色"
            android:layout_toStartOf="@id/text_eraser"
            android:layout_marginEnd="10dp"
            android:layout_centerVertical="true"
            android:textColor="@android:color/white"
            android:textSize="16sp"/>
 
        <TextView
            android:id="@+id/text_red"
            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>
    <ImageView
        android:id="@+id/image"
        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: Paint
private var canvas: Canvas
private var bitmap: Bitmap
private var imageView:ImageView
private var startX = 0f
private 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.x
                startY = event.y
            }
 
            MotionEvent.ACTION_MOVE -> {
                val endX = event.x
                val endY = event.y
                canvas.drawLine(startX, startY, endX, endY, defaultPaint)
                startX = event.x
                startY = event.y
                imageView.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,傳進(jìn)來(lái)的bitmap的寬高就是imageView的寬高。然后是初始化canvas,paint。接下來(lái)是監(jiān)聽(tīng)imageView的觸摸事件。

當(dāng)手指點(diǎn)擊屏幕時(shí)記錄下xy軸的位置,手指移動(dòng)時(shí)只需要調(diào)用canvas的drawLine方法就可以畫出一條線了。給drawLine方法傳遞初始位置,現(xiàn)在的位置和一個(gè)paint參數(shù),我們可以控制畫筆的粗細(xì)程度,顏色等。這里有朋友們可能會(huì)想,我調(diào)用的是canvas的drawLine方法,這和bitmap有什么關(guān)系呢?其實(shí)我們畫的就是一個(gè)個(gè)像素點(diǎn)組成的位圖,用bitmap來(lái)存儲(chǔ)這些像素點(diǎn)。drawLine方法的任務(wù)就是把這些像素點(diǎn)記錄在bitmap上面。最后就是把bitmap傳給imageView顯示出來(lái)。

MainActivity

package com.example.drawline
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import 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() }
    }
}
}

因?yàn)閯?chuàng)建bitmap時(shí)我們傳遞的了imageView的寬高,如果image View的寬高還沒(méi)測(cè)量完就傳到bitmap里面,這時(shí)候傳遞的可能是負(fù)數(shù),這導(dǎo)致無(wú)法創(chuàng)建bitmap。所以這里先等到image View完全繪制完畢,再傳遞它的寬高即可。在網(wǎng)上看到別人用了一張背景圖,然后傳給bitmap的是這個(gè)背景圖的大小,這也是解決辦法之一。大家可以按照自己的需求選擇合理的方法就可以。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android SearchView搜索框組件的使用方法

    Android SearchView搜索框組件的使用方法

    這篇文章主要為大家詳細(xì)介紹了Android SearchView搜索框組件的使用方法,即時(shí)搜索提示功能的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android實(shí)現(xiàn)橫向滑動(dòng)卡片效果

    Android實(shí)現(xiàn)橫向滑動(dòng)卡片效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)橫向滑動(dòng)卡片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 基于Android studio3.6的JNI教程之helloworld思路詳解

    基于Android studio3.6的JNI教程之helloworld思路詳解

    這篇文章主要介紹了基于Android studio3.6的JNI教程之helloworld,本文通過(guò)圖文實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Android程序結(jié)構(gòu)簡(jiǎn)單講解

    Android程序結(jié)構(gòu)簡(jiǎn)單講解

    在本篇文章里小編給大家分享一篇關(guān)于Android程序結(jié)構(gòu)的簡(jiǎn)單說(shuō)明內(nèi)容,有需要的朋友們跟著學(xué)習(xí)下。
    2019-02-02
  • Android?懸浮按鈕之實(shí)現(xiàn)兔兔按鈕示例

    Android?懸浮按鈕之實(shí)現(xiàn)兔兔按鈕示例

    這篇文章主要為大家介紹了Android?懸浮按鈕之實(shí)現(xiàn)兔兔按鈕示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Android?獲取實(shí)時(shí)網(wǎng)速實(shí)現(xiàn)詳解

    Android?獲取實(shí)時(shí)網(wǎng)速實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了Android?獲取實(shí)時(shí)網(wǎng)速實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android 開發(fā)實(shí)例簡(jiǎn)單涂鴉板

    Android 開發(fā)實(shí)例簡(jiǎn)單涂鴉板

    本文主要介紹 Android 簡(jiǎn)單涂鴉板,這里提供了代碼示例和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下
    2016-08-08
  • Android Socket通信詳解

    Android Socket通信詳解

    這篇文章主要介紹了Android Socket通信詳解的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • Android繪制旋轉(zhuǎn)動(dòng)畫方法詳解

    Android繪制旋轉(zhuǎn)動(dòng)畫方法詳解

    這篇文章主要介紹了Android如何采用RotateAnimation繪制一個(gè)旋轉(zhuǎn)動(dòng)畫,文中的實(shí)現(xiàn)方法講解詳細(xì),感興趣的小伙伴可以跟隨小編一起試一試
    2022-01-01
  • Android相機(jī)、圖冊(cè)demo

    Android相機(jī)、圖冊(cè)demo

    這篇文章主要為大家詳細(xì)介紹了Android相機(jī)、圖冊(cè)的基本常見(jiàn)使用方法,幾乎涵蓋了Android相機(jī)、圖冊(cè)操作方法,感興趣的小伙伴們可以參考一下
    2016-04-04

最新評(píng)論