package com.example.testjni.livetext;
import android.app.Activity;
import android.graphics.RectF;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.OverScroller;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.example.testjni.R;
import com.github.chrisbanes.photoview.OnMatrixChangedListener;
import com.github.chrisbanes.photoview.OnScaleChangedListener;
import com.github.chrisbanes.photoview.OnSingleFlingListener;
import com.github.chrisbanes.photoview.OnViewDragListener;
import com.github.chrisbanes.photoview.PhotoView;
/**
* create by zlk
* on 2022/5/3
*/
public class TestLiveTextActivity extends Activity {
PhotoView view;
TestManView testManView;
private String TAG = "TestLiveTextActivity";
float scale = 1;
float offsetX = 0;
float offsetY = 0;
OverScroller overScroller;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_live);
view = findViewById(R.id.photo_view);
testManView = findViewById(R.id.test_man_view);
// 这两个参数不懂
view.setImageResource(R.mipmap.shu);
Log.e(TAG,"oncreate");
overScroller = new OverScroller(this);
view.setOnMatrixChangeListener(new MatrixChangeListener());
// 以某个点为中心扩大和缩小和平移动
//获取图片的宽和高
view.setOnScaleChangeListener(new OnScaleChangedListener() {
@Override
public void onScaleChange(float scaleFactor, float focusX, float focusY) {
// Log.e(TAG,"focusX = " + focusX + " focusY = "+focusY +
// "scaleFactor = " + scaleFactor);
scale = scale * scaleFactor;
// 控制原点
testManView.setPivotX(focusX);
testManView.setPivotY(focusY);
testManView.setScaleX(scale);
testManView.setScaleY(scale);
}
});
view.setOnSingleFlingListener(new OnSingleFlingListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d(TAG,"velocityX = " + velocityX);
Log.d(TAG,"velocityY = " + velocityY);
if(scale != 1) {
overScroller.fling((int) offsetX, (int) offsetY, (int) velocityX, (int) velocityY,
-(int) (view.getWidth() * scale - view.getWidth()) / 2,
(int) (view.getWidth() * scale - view.getWidth()) / 2,
-(int) (view.getHeight() * scale - view.getHeight()) / 2,
(int) (view.getHeight() * scale - view.getHeight()) / 2,
300, 300);
//postOnAnimation(new FlingRunner());
//view.postDelayed(new FlingRunner(), 1000 / 60);
}
// overScroller.computeScrollOffset();
// offsetX = overScroller.getCurrX();
// offsetY = overScroller.getCurrY();
return false;
}
});
//错误原因,没考虑边界,下滑可能滑动的距离很大。。操他妈
view.setOnViewDragListener(new OnViewDragListener() {
@Override
public void onDrag(float dx, float dy) {
Log.d(TAG,"dx = " + dx + " dy = " +dy);
// Log.e(TAG, "first retf =" + firstRectf.toString());
if(scale!=1) {
Log.e(TAG, "current retf =" + currentRectf.toString());
Log.e(TAG, "isChangex =" +isChangex);
Log.e(TAG, "isChangey =" +isChangey);
if(isChangex){
offsetX += dx;
testManView.setTranslationX(offsetX );
}
if(isChangey){
offsetY += dy;
testManView.setTranslationY(offsetY );
}
// offsetY += dy;
// fixOffsets();
Log.d(TAG,"offsetX = " + offsetX + " offsetY = " +offsetY);
// Log.d(TAG,"scale = " + scale);
}
}
});
//
// view.setOnScrollChangeListener(new View.OnScrollChangeListener() {
// @Override
// public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
// Log.d(TAG,"scrollX = " + scrollX);
// Log.d(TAG,"scrollY = " + scrollY);
// }
// });
}
class FlingRunner implements Runnable {
@Override
public void run() {
if (overScroller.computeScrollOffset()) {
offsetX = overScroller.getCurrX();
offsetY = overScroller.getCurrY();
testManView.setTranslationX(offsetX * scale);
testManView.setTranslationY(offsetY * scale);
// 下一帧动画的时候执行
view.postDelayed(this, 1000/60);
}
}
}
// 计算边界,区间算错了算大了
private void fixOffsets() {
// offsetX = Math.min(offsetX, currentRectf.left - firstRectf.left);
// offsetX = Math.max(offsetX, currentRectf.right - firstRectf.right );
// offsetY = Math.min(offsetY, currentRectf.top - firstRectf.top );
// offsetY = Math.max(offsetY,currentRectf.bottom - firstRectf.bottom );
//
// Log.e(TAG,"left = "+ (currentRectf.left - firstRectf.left));
if(currentRectf.left == 0){
}
}
RectF firstRectf,currentRectf = new RectF(1,1,1,1);
boolean isChangex,isChangey;
//构建一个最大的可移动juxing
private class MatrixChangeListener implements OnMatrixChangedListener {
@Override
public void onMatrixChanged(RectF rect) {
if(currentRectf == null){
currentRectf = rect;
}else{
// if(rect.left <firstRectf.left){
// firstRectf.left = rect.left;
// }
// if(rect.right >firstRectf.right){
// firstRectf.right = rect.right;
// }
// if(rect.top <firstRectf.top){
// firstRectf.top = rect.top;
// }
// if(rect.bottom >firstRectf.bottom){
// firstRectf.bottom = rect.bottom;
// }
//currentRectf.top != rect.top || currentRectf.bottom != rect.bottom
isChangex = false;
isChangey = false;
if( currentRectf.left != rect.left && currentRectf.right != rect.right) {
currentRectf.left = rect.left;
currentRectf.right = rect.right;
isChangex = true;
}
if(currentRectf.top != rect.top && currentRectf.bottom != rect.bottom) {
currentRectf.top = rect.top;
currentRectf.bottom = rect.bottom;
isChangey = true;
}
}
}
}
}
|