Android放大鏡的實現

 

    • Java代碼
      1. view plaincopy to clipboardprint?  
      2.   
      3.   
      4.   
      5. package chroya.demo.magnifier;   
      6.   
      7.   
      8. import android.content.Context;   
      9. import android.graphics.Bitmap;   
      10. import android.graphics.BitmapFactory;   
      11. import android.graphics.BitmapShader;   
      12. import android.graphics.Canvas;   
      13. import android.graphics.Matrix;   
      14. import android.graphics.Shader.TileMode;   
      15. import android.graphics.drawable.ShapeDrawable;   
      16. import android.graphics.drawable.shapes.OvalShape;   
      17. import android.view.MotionEvent;   
      18. import android.view.View;   
      19.   
      20.   
      21. /**  
      22. * 放大鏡實現方式1  
      23. * @author chroya  
      24.  
      25. */  
      26.   
      27. public  
      28. class ShaderView extends View{   
      29. private Bitmap bitmap;   
      30. private ShapeDrawable drawable;   
      31. //放大鏡的半徑   
      32.   
      33. private  
      34. static  
      35. final  
      36. int RADIUS = 80;   
      37. //放大倍數   
      38.   
      39. private  
      40. static  
      41. final  
      42. int FACTOR = 3;   
      43. private Matrix matrix = new Matrix();   
      44.   
      45.   
      46. public ShaderView(Context context) {   
      47. super(context);   
      48. Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.show);   
      49. bitmap = bmp;   
      50. BitmapShader shader = new BitmapShader(   
      51. Bitmap.createScaledBitmap(bmp, bmp.getWidth()*FACTOR,   
      52. bmp.getHeight()*FACTOR, true), TileMode.CLAMP, TileMode.CLAMP);   
      53. //圓形的drawable   
      54.   
      55. drawable = new ShapeDrawable(new OvalShape());   
      56. drawable.getPaint().setShader(shader);   
      57. drawable.setBounds(00, RADIUS*2, RADIUS*2);   
      58. }   
      59.   
      60.   
      61. @Override  
      62.   
      63. public  
      64. boolean onTouchEvent(MotionEvent event) {   
      65. final  
      66. int x = (int) event.getX();   
      67. final  
      68. int y = (int) event.getY();   
      69. //這個位置表示的是,畫shader的起始位置   
      70.   
      71. matrix.setTranslate(RADIUS-x*FACTOR, RADIUS-y*FACTOR);   
      72. drawable.getPaint().getShader().setLocalMatrix(matrix);   
      73. //bounds,就是那個圓的外切矩形   
      74.   
      75. drawable.setBounds(x-RADIUS, y-RADIUS, x+RADIUS, y+RADIUS);   
      76. invalidate();   
      77. return  
      78. true;   
      79. }   
      80.   
      81.   
      82. @Override  
      83.   
      84. public  
      85. void onDraw(Canvas canvas) {   
      86. super.onDraw(canvas);   
      87. canvas.drawBitmap(bitmap, 00null);   
      88. drawable.draw(canvas);   
      89. }   
      90. }  

       

基本原理就是使用ShapeDrawable構造一個圓形的drawable,然後它的paint的shader設置爲將要放大的圖片,然後就是簡單的位置移動問題了。放大鏡的半徑和放大倍數都可以在代碼裏面修改,代碼都有註釋,應該很好理解了。

不過,一個問題如果只有一種解決方法的話,那未免有點令人沮喪,想玩點另類的都不行。
玩程序就得玩出個性,玩出激情。哈哈,廢話太多,切回正題。


再來看看放大鏡的另外一種實現吧
Java代碼
    • view plaincopy to clipboardprint?  
    •   
    •   
    • package chroya.demo.magnifier;   
    •   
    •   
    • import android.content.Context;   
    • import android.graphics.Bitmap;   
    • import android.graphics.BitmapFactory;   
    • import android.graphics.Canvas;   
    • import android.graphics.Matrix;   
    • import android.graphics.Path;   
    • import android.graphics.Path.Direction;   
    • import android.view.MotionEvent;   
    • import android.view.View;   
    •   
    •   
    • /**  
    • * 放大鏡實現方式2  
    • * @author chroya  
    •  
    • */  
    •   
    • public  
    • class PathView extends View{   
    • private Path mPath = new Path();   
    • private Matrix matrix = new Matrix();   
    • private Bitmap bitmap;   
    • //放大鏡的半徑   
    •   
    • private  
    • static  
    • final  
    • int RADIUS = 80;   
    • //放大倍數   
    •   
    • private  
    • static  
    • final  
    • int FACTOR = 2;   
    • private  
    • int mCurrentX, mCurrentY;   
    •   
    •   
    • public PathView(Context context) {   
    • super(context);   
    • mPath.addCircle(RADIUS, RADIUS, RADIUS, Direction.CW);   
    • matrix.setScale(FACTOR, FACTOR);   
    •   
    •   
    • bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.show);   
    • }   
    •   
    •   
    • @Override  
    •   
    • public  
    • boolean onTouchEvent(MotionEvent event) {   
    • mCurrentX = (int) event.getX();   
    • mCurrentY = (int) event.getY();   
    •   
    •   
    • invalidate();   
    • return  
    • true;   
    • }   
    •   
    •   
    • @Override  
    •   
    • public  
    • void onDraw(Canvas canvas) {   
    • super.onDraw(canvas);   
    • //底圖   
    •   
    • canvas.drawBitmap(bitmap, 00null);   
    • //剪切   
    •   
    • canvas.translate(mCurrentX - RADIUS, mCurrentY - RADIUS);   
    • canvas.clipPath(mPath);   
    • //畫放大後的圖   
    •   
    • canvas.translate(RADIUS-mCurrentX*FACTOR, RADIUS-mCurrentY*FACTOR);   
    • canvas.drawBitmap(bitmap, matrix, null);   
    • }   
    • }  


這裏使用的是Path類,將canvas剪切出一塊圓形區域,在其上繪製放大的部分。
兩種方式的效果都一樣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章