xml
<ImageView
android:id="@+id/know_star"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="matrix"
android:src="@drawable/earth_know"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
activity
private Matrix mMatrix = new Matrix();
private float mScaleFactor = .4f;
private float mRotationDegrees = 0.f;
private float mFocusX = 0.f;
private float mFocusY = 0.f;
private int mImageHeight, mImageWidth;
private ParticleSystem ps;
private ScaleGestureDetector mScaleDetector;
private RotateGestureDetector mRotateDetector;
private ImageView knowHelp;
@SuppressLint("UseCompatLoadingForDrawables")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_knowledge);
initView();
Display display = getWindowManager().getDefaultDisplay();
mFocusX = display.getWidth() / 2f;
mFocusY = display.getHeight() / 3f;
Drawable d = this.getResources().getDrawable(stars[star - 1]);
mImageHeight = d.getIntrinsicHeight();
mImageWidth = d.getIntrinsicWidth();
float scaledImageCenterX = (mImageWidth * mScaleFactor) / 2;
float scaledImageCenterY = (mImageHeight * mScaleFactor) / 2;
mMatrix.postScale(mScaleFactor, mScaleFactor);
mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);
knowStar.setImageMatrix(mMatrix);
mScaleDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleListener());
mRotateDetector = new RotateGestureDetector(getApplicationContext(), new RotateListener());
}
@SuppressLint("ClickableViewAccessibility")
private void initView() {
knowBack = (ImageView) findViewById(R.id.know_back);
knowStar = (ImageView) findViewById(R.id.know_star);
knowKnow = (ImageView) findViewById(R.id.know_know);
knowStar.setOnTouchListener(this);
knowHelp = (ImageView) findViewById(R.id.know_help);
}
@Override
protected void onPause() {
super.onPause();
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
}
@SuppressLint("ClickableViewAccessibility")
public boolean onTouch(View v, MotionEvent event) {
knowHelp.setVisibility(View.GONE);
mScaleDetector.onTouchEvent(event);
mRotateDetector.onTouchEvent(event);
float scaledImageCenterX = (mImageWidth * mScaleFactor) / 2;
float scaledImageCenterY = (mImageHeight * mScaleFactor) / 2;
mMatrix.reset();
mMatrix.postScale(mScaleFactor, mScaleFactor);
mMatrix.postRotate(mRotationDegrees, scaledImageCenterX, scaledImageCenterY);
mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);
ImageView view = (ImageView) v;
view.setImageMatrix(mMatrix);
return true;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
return true;
}
}
private class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
@Override
public boolean onRotate(RotateGestureDetector detector) {
mRotationDegrees -= detector.getRotationDegreesDelta();
return true;
}
}
}