Android画布Canvas绘图scale,Kotlin

Android画布Canvas绘图scale,Kotlin

 

 




    

    

        

        

        

    

    

        

        

        

    

 

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class MainActivity : AppCompatActivity() {
    private var iv: ImageView? = null
    private var iv1: ImageView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        iv = findViewById(R.id.iv)

        iv1 = findViewById(R.id.iv1)

        lifecycleScope.launch(Dispatchers.Main) {
            delay(500)

            f1()
        }
    }

    private fun f1() {
        val bitmap = ((iv?.drawable as BitmapDrawable).bitmap.copy(Bitmap.Config.ARGB_8888, true))
        //val bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        //canvas.drawColor(Color.LTGRAY) //铺满

        val w = bitmap.width
        val h = bitmap.height
        Log.d("fly", "w=$w h=$h")

        val paint = Paint()
        paint.isAntiAlias = true
        paint.color = Color.RED
        paint.style = Paint.Style.FILL

        val left = 60f
        val top = 60f
        val rectF = RectF(left, top, left + w / 2, top + h / 2)
        canvas.drawRect(rectF, paint)

        canvas.scale(0.5f, 0.5f) //缩小。
        paint.color = Color.YELLOW
        canvas.drawRect(rectF, paint)

        canvas.scale(0.3f, 0.3f) //缩小。
        paint.color = Color.BLUE
        canvas.drawRect(rectF, paint)

        iv1?.setImageBitmap(bitmap)
    }
}

 

 

 

 

Android画布Canvas绘制drawBitmap基于源Rect和目的Rect,Kotlin-CSDN博客文章浏览阅读471次,点赞9次,收藏8次。文章浏览阅读9.6k次。文章浏览阅读1.8k次。/*Java代码 将Drawable转化为Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth();Android Material Design :LinearLayoutCompat添加分割线divider_linearlayout 分割线-CSDN博客。https://blog.csdn.net/zhangphil/article/details/134818221

 

你可能感兴趣的:(Android,kotlin,android,kotlin)