如何在Android中的图像上添加不同的绘图对象,如圆形,三角形和矩形

时间:2019-05-22 12:40:13

标签: android

如何在图像上绘制圆形,矩形,正方形,三角形等不同对象,添加对象后可以对其进行放大,拖动和缩小。

我目前在图像上添加了一个固定大小的圆圈。

我已经实现了以下代码,该代码在图像上添加了圆圈,但是我无法在图像上放大,缩小或拖动对象。

ViewEditImage:AppCompatActivity()类,View.OnClickListener {

internal var bitmap: Bitmap? = null
internal var  mutableBitmap:Bitmap? = null
private var activity: Activity? = null
/*Toolbar*/
private var toolbar: Toolbar? = null
private var toolbarTitle: TextView? = null
private var then: Long = 0
private val matrix = Matrix()
private val savedMatrix = Matrix()
private val start = PointF()
private var mode = ViewEditImage.NONE
private var lastEvent: FloatArray? = null

companion object {
    var bitmap: Bitmap? = null
    var resultfinal: Bitmap? = null
    // we can be in one of these 3 states
    private val NONE = 0
    private val DRAG = 1
    private val ZOOM = 2
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.edit_image_activity)
    activity = this

    val myUri = Uri.parse(intent.getStringExtra("BitmapImage"))
     bitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, myUri)
    bitmap = Bitmap.createScaledBitmap(bitmap, 240, 240, false)

    Glide.with(activity!!)
        .load(myUri)
        .into(editImageView)
    editImageView.setScaleType(ImageView.ScaleType.FIT_XY);


    initializeHeader()
    init()

}

private fun initializeHeader() {
    toolbar = findViewById(R.id.toolbar)
    toolbarTitle = findViewById(R.id.txt_toolbar_title)
    setSupportActionBar(toolbar);
    supportActionBar!!.setDisplayShowTitleEnabled(false)
    toolbarTitle!!.text = resources.getString(R.string.editImage)
}

fun init(){
    rotateRightImageButton.setOnClickListener(this)
    rotateLeftImageButton.setOnClickListener(this)
    circleButton.setOnClickListener(this)

}

override fun onClick(v: View?) {
    if(v==rotateRightImageButton){
        val matrix = Matrix()
        matrix.postRotate(90f)

       bitmap = Bitmap.createBitmap(
            bitmap, 0, 0, bitmap!!.getWidth(), bitmap!!.getHeight(),
            matrix, true
        )


        editImageView.setImageBitmap(bitmap)
        editImageView.setScaleType(ImageView.ScaleType.FIT_XY);
    }
    if(v==rotateLeftImageButton){
        val matrix = Matrix()
        matrix.postRotate(-90f)

        bitmap = Bitmap.createBitmap(
            bitmap, 0, 0, bitmap!!.getWidth(), bitmap!!.getHeight(),
            matrix, true
        )


        editImageView.setImageBitmap(bitmap)
        editImageView.setScaleType(ImageView.ScaleType.FIT_XY);
    }
    if(v == circleButton){

        val myOptions = BitmapFactory.Options()
        myOptions.inDither = true
        myOptions.inScaled = false
        myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888
        myOptions.inPurgeable = true


        val paint = Paint()
        paint.isAntiAlias = true
        paint.color = Color.BLUE


        val workingBitmap = Bitmap.createBitmap(bitmap)
        mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888,         true)


        val canvas = Canvas(mutableBitmap)
        canvas.drawCircle(60f, 50f, 25f, paint)

        editImageView.adjustViewBounds = true
        editImageView.setImageBitmap(mutableBitmap)

    }
}

}

0 个答案:

没有答案
相关问题