Skip to content

Commit

Permalink
修复自定义尺寸时重绘问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wzgl5533 committed Sep 21, 2018
1 parent bb3aee4 commit e812550
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 61 deletions.
38 changes: 6 additions & 32 deletions app/src/main/java/com/qlh/crop/cropview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.qlh.crop.cropview
import android.graphics.BitmapFactory
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*


Expand All @@ -12,43 +15,14 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

//rect()
cirCle()
}

private fun cirCle(){
circle_view.setSourceBitmap(BitmapFactory.decodeResource(resources,R.drawable.d))
circle_view.setCropBitmapCallBack {
crop_view.setSourceBitmap(BitmapFactory.decodeResource(resources,R.drawable.d))
crop_view.setCropBitmapCallBack {
crop_img.setImageBitmap(it)
Toast.makeText(this,"裁剪完成",Toast.LENGTH_SHORT).show()
}
}

private fun rect(){
val b = BitmapFactory.decodeResource(resources,R.drawable.d)
// //图片尺寸
// val pw=b.width
// val ph=b.height
// val vto = bg.viewTreeObserver
// vto.addOnPreDrawListener(object :ViewTreeObserver.OnPreDrawListener{
// override fun onPreDraw(): Boolean {
// //TODO("not implemented")
// //imgView尺寸
// val imgW = bg.width
// val imgH = bg.height
// //比例
// val bx = imgW*1.0f/pw
// val bh = imgH*1.0f/ph
// //取最小值
// val lp = RelativeLayout.LayoutParams((min(bx,bh)*pw).toInt(), (min(bx,bh)*ph).toInt())
// lp.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE)
// cut_rect.layoutParams = lp
// return true
// }
// })

// cut_rect.setCropBitmapCallBack {
// crop_img.setImageBitmap(it)
// }
}

}
18 changes: 10 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@
<!--一般全屏,要和裁剪框尺寸匹配-->
<ImageView
android:id="@+id/bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:layout_width="500dp"
android:layout_height="500dp"
android:scaleType="fitXY"
android:layout_centerInParent="true"
android:src="@drawable/d"/>
<ImageView
android:id="@+id/crop_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerInside"
android:background="@color/white"/>

<com.qlh.crop.cropviewlibrary.view.CropView
android:visibility="visible"
android:id="@+id/circle_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/crop_view"
android:layout_width="500dp"
android:layout_height="500dp"
android:layout_centerInParent="true"
app:cv_border_line_color="@color/white"
app:cv_corner_line_color="@color/white"
app:cv_middle_line_color="@color/white"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

Expand All @@ -22,6 +23,9 @@
/**
* 作者:dell on 2018/9/7 18:26
* 描述:绘制拍照推荐裁剪框
* <br>
* 本库目前只是简单的绘制和裁剪
* </br>
*/
public class CropView extends View {

Expand Down Expand Up @@ -239,6 +243,11 @@ else if (POINT_STATE == 1) {
return true;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

/**
* 初始化布局
*
Expand All @@ -251,27 +260,9 @@ else if (POINT_STATE == 1) {
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);

//总高和总宽需要综合考虑view的宽和高和图片的宽和高
//但是由于异步机制,无法在onLayout之前获得偏移量,因此图片溢出改为消息提醒
/*
if(imageHeight==0){
VIEW_HEIGHT = this.getHeight();
}else{
if(imageHeight<this.getHeight())
VIEW_HEIGHT=imageHeight;
else
VIEW_HEIGHT = this.getHeight()+getTranslationY();
}
if(imageWidth==0){
VIEW_WIDTH = this.getWidth();
}else{
if(imageWidth<this.getWidth())
VIEW_WIDTH=imageWidth;
else
VIEW_WIDTH = this.getWidth()+getTranslationX();
}
*/
//如果没有改变不用重新绘制,否则会导致移动裁剪框时重绘,不能保持当前状态
if (!changed){return;}
//获取视图尺寸
VIEW_HEIGHT = this.getHeight();
VIEW_WIDTH = this.getWidth();
//初始化四个点的坐标
Expand Down

0 comments on commit e812550

Please sign in to comment.