You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.5 KiB
Java
80 lines
2.5 KiB
Java
package com.xypower.mpapp.v2;
|
|
|
|
import android.content.Context;
|
|
import android.opengl.GLSurfaceView;
|
|
import android.util.AttributeSet;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
|
|
public class AutoFitGLView extends GLSurfaceView implements View.OnTouchListener {
|
|
|
|
private int mRatioWidth = 0;
|
|
private int mRatioHeight = 0;
|
|
|
|
public AutoFitGLView(Context context) {
|
|
this(context, null);
|
|
}
|
|
|
|
public AutoFitGLView(Context context, AttributeSet attrs) {
|
|
super(context, attrs);
|
|
setOnTouchListener(this);
|
|
}
|
|
|
|
private TouchListener touchListener;
|
|
|
|
/**
|
|
* Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
|
|
* calculated from the parameters. Note that the actual sizes of parameters don't matter, that
|
|
* is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
|
|
*
|
|
* @param width Relative horizontal size
|
|
* @param height Relative vertical size
|
|
*/
|
|
public void setAspectRatio(int width, int height) {
|
|
if (width < 0 || height < 0) {
|
|
throw new IllegalArgumentException("Size cannot be negative.");
|
|
}
|
|
mRatioWidth = width;
|
|
mRatioHeight = height;
|
|
post(() -> requestLayout());
|
|
}
|
|
|
|
|
|
@Override
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
final int actionMasked = event.getActionMasked();
|
|
if (actionMasked != MotionEvent.ACTION_DOWN) {
|
|
return false;
|
|
}
|
|
|
|
if (touchListener != null) {
|
|
touchListener.onTouch(event, v.getWidth(), v.getHeight());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public interface TouchListener {
|
|
void onTouch(MotionEvent event, int width, int height);
|
|
}
|
|
|
|
public void setTouchListener(TouchListener touchListener) {
|
|
this.touchListener = touchListener;
|
|
}
|
|
|
|
@Override
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
int width = MeasureSpec.getSize(widthMeasureSpec);
|
|
int height = MeasureSpec.getSize(heightMeasureSpec);
|
|
if (0 == mRatioWidth || 0 == mRatioHeight) {
|
|
setMeasuredDimension(width, height);
|
|
} else {
|
|
if (width < height * mRatioWidth / mRatioHeight) {
|
|
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
|
|
} else {
|
|
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
|
|
}
|
|
}
|
|
}
|
|
}
|