Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. import android.content.Intent;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6.  
  7. public class specbuttons extends Activity {
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_specbuttons);
  13.  
  14. TouchImageBroken img = new TouchImageBroken(this);
  15. img.setImageResource(R.drawable.specpc14p2);
  16. img.setMaxZoom(10f);
  17. setContentView(img);
  18.  
  19.  
  20. }
  21.  
  22.  
  23.  
  24.  
  25. }
  26.  
  27. <?xml version="1.0" encoding="utf-8"?>
  28. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  29. xmlns:tools="http://schemas.android.com/tools"
  30. android:layout_width="match_parent"
  31. android:layout_height="match_parent"
  32. android:paddingBottom="@dimen/activity_vertical_margin"
  33. android:paddingLeft="@dimen/activity_horizontal_margin"
  34. android:paddingRight="@dimen/activity_horizontal_margin"
  35. android:paddingTop="@dimen/activity_vertical_margin"
  36. tools:context="com.heservices.heapp.heservices.specbuttons">
  37.  
  38. <android.support.v4.view.ViewPager
  39. android:id="@+id/viewpager"
  40. android:layout_height="500dp"
  41. android:layout_width="match_parent"/>
  42.  
  43. <ImageView
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:id="@+id/imageView7"
  47. android:src="@drawable/specpc14p2"
  48. android:layout_alignParentTop="true"
  49. android:layout_alignParentEnd="true" />
  50.  
  51. <Button
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:text=">"
  55. android:id="@+id/button3"
  56. android:layout_alignBottom="@+id/imageView7"
  57. android:layout_alignParentEnd="true" />
  58.  
  59. </RelativeLayout>
  60.  
  61. import android.content.Context;
  62. import android.graphics.Matrix;
  63. import android.graphics.PointF;
  64. import android.graphics.drawable.Drawable;
  65. import android.util.AttributeSet;
  66. import android.util.Log;
  67. import android.view.MotionEvent;
  68. import android.view.ScaleGestureDetector;
  69. import android.view.View;
  70. import android.widget.ImageView;
  71.  
  72. public class TouchImageBroken extends ImageView {
  73. Matrix matrix;
  74. // We can be in one of these 3 states
  75. static final int NONE = 0;
  76. static final int DRAG = 1;
  77. static final int ZOOM = 2;
  78.  
  79. int mode = NONE;
  80.  
  81. // Remember some things for zooming
  82. PointF last = new PointF();
  83. PointF start = new PointF();
  84. float minScale = 1f;
  85. float maxScale = 3f;
  86. float[] m;
  87. int viewWidth, viewHeight;
  88.  
  89. static final int CLICK = 3;
  90.  
  91. float saveScale = 1f;
  92.  
  93. protected float origWidth, origHeight;
  94.  
  95. int oldMeasuredWidth, oldMeasuredHeight;
  96.  
  97. ScaleGestureDetector mScaleDetector;
  98.  
  99. Context context;
  100.  
  101. public TouchImageBroken(Context context) {
  102. super(context);
  103. sharedConstructing(context);
  104. }
  105.  
  106. public TouchImageBroken(Context context, AttributeSet attrs) {
  107. super(context, attrs);
  108. sharedConstructing(context);
  109. }
  110.  
  111. private void sharedConstructing(Context context) {
  112.  
  113. super.setClickable(true);
  114.  
  115. this.context = context;
  116.  
  117. mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
  118.  
  119. matrix = new Matrix();
  120.  
  121. m = new float[9];
  122.  
  123. setImageMatrix(matrix);
  124.  
  125. setScaleType(ScaleType.MATRIX);
  126.  
  127. setOnTouchListener(new OnTouchListener() {
  128.  
  129. @Override
  130. public boolean onTouch(View v, MotionEvent event) {
  131.  
  132. mScaleDetector.onTouchEvent(event);
  133.  
  134. PointF curr = new PointF(event.getX(), event.getY());
  135.  
  136. switch (event.getAction()) {
  137.  
  138. case MotionEvent.ACTION_DOWN:
  139.  
  140. last.set(curr);
  141.  
  142. start.set(last);
  143.  
  144. mode = DRAG;
  145.  
  146. break;
  147.  
  148. case MotionEvent.ACTION_MOVE:
  149.  
  150. if (mode == DRAG) {
  151.  
  152. float deltaX = curr.x - last.x;
  153.  
  154. float deltaY = curr.y - last.y;
  155.  
  156. float fixTransX = getFixDragTrans(deltaX, viewWidth, origWidth * saveScale);
  157.  
  158. float fixTransY = getFixDragTrans(deltaY, viewHeight, origHeight * saveScale);
  159.  
  160. matrix.postTranslate(fixTransX, fixTransY);
  161.  
  162. fixTrans();
  163.  
  164. last.set(curr.x, curr.y);
  165.  
  166. }
  167.  
  168. break;
  169.  
  170. case MotionEvent.ACTION_UP:
  171.  
  172. mode = NONE;
  173.  
  174. int xDiff = (int) Math.abs(curr.x - start.x);
  175.  
  176. int yDiff = (int) Math.abs(curr.y - start.y);
  177.  
  178. if (xDiff < CLICK && yDiff < CLICK)
  179.  
  180. performClick();
  181.  
  182. break;
  183.  
  184. case MotionEvent.ACTION_POINTER_UP:
  185.  
  186. mode = NONE;
  187.  
  188. break;
  189.  
  190. }
  191.  
  192. setImageMatrix(matrix);
  193.  
  194. invalidate();
  195.  
  196. return true; // indicate event was handled
  197.  
  198. }
  199.  
  200. });
  201. }
  202.  
  203. public void setMaxZoom(float x) {
  204.  
  205. maxScale = x;
  206.  
  207. }
  208.  
  209. private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
  210.  
  211. @Override
  212. public boolean onScaleBegin(ScaleGestureDetector detector) {
  213.  
  214. mode = ZOOM;
  215.  
  216. return true;
  217.  
  218. }
  219.  
  220. @Override
  221. public boolean onScale(ScaleGestureDetector detector) {
  222.  
  223. float mScaleFactor = detector.getScaleFactor();
  224.  
  225. float origScale = saveScale;
  226.  
  227. saveScale *= mScaleFactor;
  228.  
  229. if (saveScale > maxScale) {
  230.  
  231. saveScale = maxScale;
  232.  
  233. mScaleFactor = maxScale / origScale;
  234.  
  235. } else if (saveScale < minScale) {
  236.  
  237. saveScale = minScale;
  238.  
  239. mScaleFactor = minScale / origScale;
  240.  
  241. }
  242.  
  243. if (origWidth * saveScale <= viewWidth || origHeight * saveScale <= viewHeight)
  244.  
  245. matrix.postScale(mScaleFactor, mScaleFactor, viewWidth / 2, viewHeight / 2);
  246.  
  247. else
  248.  
  249. matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY());
  250.  
  251. fixTrans();
  252.  
  253. return true;
  254.  
  255. }
  256.  
  257. }
  258.  
  259. void fixTrans() {
  260.  
  261. matrix.getValues(m);
  262.  
  263. float transX = m[Matrix.MTRANS_X];
  264.  
  265. float transY = m[Matrix.MTRANS_Y];
  266.  
  267. float fixTransX = getFixTrans(transX, viewWidth, origWidth * saveScale);
  268.  
  269. float fixTransY = getFixTrans(transY, viewHeight, origHeight * saveScale);
  270.  
  271. if (fixTransX != 0 || fixTransY != 0)
  272.  
  273. matrix.postTranslate(fixTransX, fixTransY);
  274.  
  275. }
  276.  
  277.  
  278.  
  279. float getFixTrans(float trans, float viewSize, float contentSize) {
  280.  
  281. float minTrans, maxTrans;
  282.  
  283. if (contentSize <= viewSize) {
  284.  
  285. minTrans = 0;
  286.  
  287. maxTrans = viewSize - contentSize;
  288.  
  289. } else {
  290.  
  291. minTrans = viewSize - contentSize;
  292.  
  293. maxTrans = 0;
  294.  
  295. }
  296.  
  297. if (trans < minTrans)
  298.  
  299. return -trans + minTrans;
  300.  
  301. if (trans > maxTrans)
  302.  
  303. return -trans + maxTrans;
  304.  
  305. return 0;
  306.  
  307. }
  308.  
  309. float getFixDragTrans(float delta, float viewSize, float contentSize) {
  310.  
  311. if (contentSize <= viewSize) {
  312.  
  313. return 0;
  314.  
  315. }
  316.  
  317. return delta;
  318.  
  319. }
  320.  
  321. @Override
  322. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  323.  
  324. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  325.  
  326. viewWidth = MeasureSpec.getSize(widthMeasureSpec);
  327.  
  328. viewHeight = MeasureSpec.getSize(heightMeasureSpec);
  329.  
  330. //
  331. // Rescales image on rotation
  332. //
  333. if (oldMeasuredHeight == viewWidth && oldMeasuredHeight == viewHeight
  334.  
  335. || viewWidth == 0 || viewHeight == 0)
  336.  
  337. return;
  338.  
  339. oldMeasuredHeight = viewHeight;
  340.  
  341. oldMeasuredWidth = viewWidth;
  342.  
  343. if (saveScale == 1) {
  344.  
  345. //Fit to screen.
  346.  
  347. float scale;
  348.  
  349. Drawable drawable = getDrawable();
  350.  
  351. if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0)
  352.  
  353. return;
  354.  
  355. int bmWidth = drawable.getIntrinsicWidth();
  356.  
  357. int bmHeight = drawable.getIntrinsicHeight();
  358.  
  359. Log.d("bmSize", "bmWidth: " + bmWidth + " bmHeight : " + bmHeight);
  360.  
  361. float scaleX = (float) viewWidth / (float) bmWidth;
  362.  
  363. float scaleY = (float) viewHeight / (float) bmHeight;
  364.  
  365. scale = Math.min(scaleX, scaleY);
  366.  
  367. matrix.setScale(scale, scale);
  368.  
  369. // Center the image
  370.  
  371. float redundantYSpace = (float) viewHeight - (scale * (float) bmHeight);
  372.  
  373. float redundantXSpace = (float) viewWidth - (scale * (float) bmWidth);
  374.  
  375. redundantYSpace /= (float) 2;
  376.  
  377. redundantXSpace /= (float) 2;
  378.  
  379. matrix.postTranslate(redundantXSpace, redundantYSpace);
  380.  
  381. origWidth = viewWidth - 2 * redundantXSpace;
  382.  
  383. origHeight = viewHeight - 2 * redundantYSpace;
  384.  
  385. setImageMatrix(matrix);
  386.  
  387. }
  388.  
  389. fixTrans();
  390.  
  391. }
  392.  
  393. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement