2015-03-03 52 views
1

我想在我的android應用程序中實現放大鏡。在觸摸位應該放大到一定比例並呈aside.I'm沒有得到在那裏我談到bitmap.Can任何確切的位圖區域給我一個solution.This是我的代碼如何在android中放大位圖的觸點?

Bitmap bitmap = new BitmapShader(modelBitmap, TileMode.CLAMP, TileMode.CLAMP); 
Paint paint = new Paint(); 
matrix1.postScale(2f, 2f, mouseDownX, mouseDownY); 
bitmap.setLocalMatrix(matrix1); 
paint.setShader(bitmap); 
canvas.drawCircle(60,230, (int) (width * 0.10), paint); 

這裏mouseDownX和mouseDownY是觸摸座標。我沒有得到我觸摸的位圖的確切區域。

+0

請張貼你已經嘗試過的代碼,並解釋不工作。如果您希望開發人員爲您編寫此代碼,則可能會花費您的資金,這可能不適合您。 – 2015-03-03 09:22:46

+0

告訴我我做錯了什麼? – user3305353 2015-03-03 09:43:21

回答

0
BitmapShader bitmap = new BitmapShader(modelBitmap, TileMode.CLAMP, 
TileMode.CLAMP); 
Paint paint = new Paint(); 
matrix1.postScale(2f, 2f, mouseDownX, mouseDownY); 
bitmap.setLocalMatrix(matrix1); 
paint.setShader(bitmap); 
canvas.drawCircle(mouseDownX, mouseDownY, (int) (width * 0.10), paint); 

請參閱How to magnify/zoom part of image

0

創建自己的類這將延長ImageView類並覆蓋onTouchEventonTouchEvent方法。獲取點擊的位置並以任何你想要的形狀在畫布上繪製觸摸的部分。

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapShader; 
import android.graphics.Canvas; 
import android.graphics.Matrix; 
import android.graphics.Paint; 
import android.graphics.PointF; 
import android.graphics.Shader; 
import android.support.v7.widget.AppCompatButton; 
import android.support.v7.widget.AppCompatImageView; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.widget.ImageView; 


public class CodesforMagnifierImageView extends AppCompatImageView 
{ 

    private PointF zoomPosition; 
    private boolean zooming = false; 
    private Matrix matrix; 
    private Paint paint; 
    private Bitmap bitmap; 
    private BitmapShader shader; 
    private int sizeOfMagnifier = 200; 

    public CodesforMagnifierImageView(Context context) 
    { 
     super(context); 
     init(); 
    } 

    public CodesforMagnifierImageView(Context context, AttributeSet attrs, int defStyleAttr) 
    { 
     super(context, attrs, defStyleAttr); 
     init(); 
    } 

    public CodesforMagnifierImageView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     init(); 
    } 

    private void init() 
    { 
     zoomPosition = new PointF(0, 0); 
     matrix = new Matrix(); 
     paint = new Paint(); 

    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) 
    { 
     int action = event.getAction(); 

     zoomPosition.x = event.getX(); 
     zoomPosition.y = event.getY(); 

     switch (action) 
     { 
      case MotionEvent.ACTION_DOWN: 
      case MotionEvent.ACTION_MOVE: 
       zooming = true; 
       this.invalidate(); 
       break; 
      case MotionEvent.ACTION_UP: 
      case MotionEvent.ACTION_CANCEL: 
       zooming = false; 
       this.invalidate(); 
       break; 

      default: 
       break; 
     } 

     return true; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     super.onDraw(canvas); 
     if (!zooming) 
     { 
      buildDrawingCache(); 
     } 
     else 
     { 

      bitmap = getDrawingCache(); 
      shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 

      paint = new Paint(); 
      paint.setShader(shader); 
      matrix.reset(); 
      matrix.postScale(2f, 2f, zoomPosition.x, zoomPosition.y); 
      paint.getShader().setLocalMatrix(matrix); 
      canvas.drawCircle(zoomPosition.x, zoomPosition.y, sizeOfMagnifier, paint); 
     } 
    } 


} 

使用它在下列方式:

<com.utilities.CodesforMagnifierImageView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/image" /> 
相關問題