可旋转放大截屏自定义View

class ZoomContainView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = -1
) : FrameLayout(context, attrs, defStyleAttr) {
    val TAG = "ZoomContainView"


    private fun init() {
        var zoomView = ZoomView(context)
        zoomView.setBackgroundResource(R.color.black)
        zoomView.id = R.id.zoom_view
        zoomView.layoutParams = LayoutParams(-1, -1).apply {
            width = LayoutParams.MATCH_PARENT
            height = LayoutParams.MATCH_PARENT
            layoutParams = this
        }
        var img = ImageView(context)
        img.scaleType = ImageView.ScaleType.FIT_CENTER
        img.id = R.id.img_zoom
        zoomView.addView(img, -1, -1)
        addView(zoomView)
    }

    fun loadImgUrl(url: Any) {
        findViewById(R.id.zoom_view).run {
            scaleX = 1f
            scaleY = 1f
            translationX = 0f
            translationY = 0f
            rotation = 0f
        }
        var img = findViewById(R.id.img_zoom)
        Glide.with(context).asBitmap().load(url).apply(
                RequestOptions().placeholder(R.color.black))
                .addListener(object : RequestListener {
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, isFirstResource: Boolean): Boolean {
                        return false
                    }
                    override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                        Log.i(TAG, resource?.width.toString() + "==" + resource?.height)
                        Log.i(TAG, width.toString() + "===" + height + "==" + measuredHeight)
                        var scale = 2f
                        if (resource?.width == width) {
                            scale = height.toFloat().div(resource?.height)
                        } else if (resource?.height == height) {
                            scale = width.toFloat().div(resource?.width)
                        }
                        findViewById(R.id.zoom_view).run {
                            scaleX = scale
                            scaleY = scale
                            setDefaultScale(scale)
                        }
                        return false

                    }
                }).into(img)
    }

    public fun reset(){
        findViewById(R.id.zoom_view)?.reset()
    }

    interface Result {
        fun success(filpath: String)
        fun error(t: Throwable?)
    }

    /*
    * filePath 可以唯空 空就是默认值
    * */
    fun doScreenShot(filePath: String?, width:Int,height:Int,result: Result) {
        ktIO {
            this.isDrawingCacheEnabled = true
            this.buildDrawingCache()
            val bitmap = Bitmap.createBitmap(this.drawingCache)
            if (bitmap != null) {
                val scaleX = width / bitmap.width.toFloat()
                val scaleY = height / bitmap.height.toFloat()
                var matrix=Matrix()
                matrix.postScale(scaleX,scaleY);
                val createBitmap = Bitmap.createBitmap( bitmap,0,0,bitmap.width,bitmap.height,matrix,true)
                saveBitmap(createBitmap, filePath, result)
                this.destroyDrawingCache()
            }
        }
    }

    @SuppressLint("WrongThread")
    @Throws(IOException::class)
    private fun saveBitmap(bmp: Bitmap, filePath: String?, result: Result) {
        try {
            var dirPath = filePath ?: context.getFilesDir().path + "/zoomimg/"
            val childFolder = File(dirPath)
            if (!childFolder.exists() || childFolder.isFile) {
                try {
                    childFolder.mkdirs();
                } catch (e: java.lang.Exception) {
                }
            }
            var zoomFile = File(dirPath + "zoomimg" + TimeUtils.getCurrentTime() + Random().nextInt(99999) + ".jpeg")
            if (zoomFile.exists()) {
                zoomFile.delete()
            }
            try {
                zoomFile.createNewFile()
            } catch (e: java.lang.Exception) {
            }
            val fOut: OutputStream = FileOutputStream(zoomFile)
            bmp.compress(Bitmap.CompressFormat.JPEG, 60, fOut) //将bg输出至文件
            fOut.flush()
            fOut.close()
            result?.success(zoomFile?.path) // do not forget to close thestream
        } catch (e: java.lang.Exception) {
            result?.error(e)
        }
    }

    init {
        init()
    }
}
使用方法loadImgUrl来加载想要操作 放大缩小旋转的图片 
doScreenShot方法用来获取截图

reset方法用来重置

你可能感兴趣的:(可旋转放大截屏自定义View)