2014-09-26 49 views
1

我可以放大縮小圖片,現在我想要做的是保存圖像zommed保存圖像查看放大之後在我的片段

我怎樣才能做到這一點

PJ 1:在頂部的圖像左邊是我從資源中選取的正常圖像,中間的第二張圖像是我想要與第一張圖像關聯的圖像。

PJ 2:變焦後,我想保存該協會在當地

 the image in the top left is the normal image i pick up from ressource, and the second image in the middle is the image i want to associate to the first imageafter the zoom i want save this association in local

類:

public class MaskFragment extends AbstractFragment{ 
    @ViewById 
    ImageView imageToEdit; 
    Matrix matrix = new Matrix(); 
    Matrix savedMatrix = new Matrix(); 
    PointF startPoint = new PointF(); 
    PointF midPoint = new PointF(); 
    float oldDist = 1f; 
    static final int NONE = 0; 
    static final int DRAG = 1; 
    static final int ZOOM = 2; 
    int mode = NONE; 

    @AfterViews 
    void initialise() { 
    /** * set on touch listner on image */ 
    imageToEdit.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      ImageView view = (ImageView) v; 
      System.out.println("matrix=" + savedMatrix.toString()); 
      switch (event.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_DOWN: 
       savedMatrix.set(matrix); 
       startPoint.set(event.getX(), event.getY()); 
       mode = DRAG; 
       break; 
      case MotionEvent.ACTION_POINTER_DOWN: 
       oldDist = spacing(event); 
       if (oldDist > 10f) { 
        savedMatrix.set(matrix); 
        midPoint(midPoint, event); 
        mode = ZOOM; 
       } 
       break; 
      case MotionEvent.ACTION_UP: 
      case MotionEvent.ACTION_POINTER_UP: 
       mode = NONE; 
       break; 
      case MotionEvent.ACTION_MOVE: 
       if (mode == DRAG) { 
        matrix.set(savedMatrix); 
        matrix.postTranslate(
         event.getX() - startPoint.x, 
         event.getY() - startPoint.y 
        ); 
       } else if (mode == ZOOM) { 
        float newDist = spacing(event); 
        if (newDist > 10f) { 
         matrix.set(savedMatrix); 
         float scale = newDist/oldDist; 
         matrix.postScale(
          scale, scale, midPoint.x, 
          midPoint.y 
         ); 
        } 
       } 
       break; 
      } 
      view.setImageMatrix(matrix); 
      return true; 
     } 

     @SuppressLint("FloatMath") 
     private float spacing(MotionEvent event) { 
      float x = event.getX(0) - event.getX(1); 
      float y = event.getY(0) - event.getY(1); 
      return FloatMath.sqrt(x * x + y * y); 
     } 

     private void midPoint(PointF point, MotionEvent event) { 
      float x = event.getX(0) + event.getX(1); 
      float y = event.getY(0) + event.getY(1); 
      point.set(x/2, y/2); 
     } 
    }); 
} 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:gesture-image="http://schemas.polites.com/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:weightSum="1" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.9" > 

    <ImageView 
     android:id="@+id/imageToEdit" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerInParent="true" 
     android:scaleType="matrix" 
     android:src="@drawable/fake_user" /> 

    <ImageView 
     android:id="@+id/mask" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scaleType="center" 
     android:layout_centerInParent="true" 
     android:src="@drawable/camera_native" /> 
</RelativeLayout> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.1" 
    android:background="@android:color/black" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="Valider" 
     android:textColor="@android:color/white" 
     android:textSize="25sp" /> 
</RelativeLayout> 

如何將第二張圖像保存在本地?

感謝您的回覆。 @Sagar Pilkhwal

  public void saveAsJpeg(View view, File file) { 
    view.setDrawingCacheEnabled(true); 
    Bitmap _b = view.getDrawingCache(); 
    OutputStream _out = null; 
    try { 
     _out = new FileOutputStream(file); 
     _b.compress(Bitmap.CompressFormat.JPEG, 100, _out);    
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     try { 
      _out.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 
+1

看看這個[SO郵報](http://stackoverflow.com/a/10364856/3326331) – 2014-09-26 08:07:37

+0

Exactelly我需要 感謝的很多。 – tamtoum1987 2014-09-26 08:36:04

回答

0

解決方案看一看位圖和文件的Android API文檔。它將幫助您瞭解如何在不復制和粘貼的情況下實現一切工作。

//use image from cache 
    yourImageView.setDrawingCacheEnabled(true); 
    yourImageView.buildDrawingCache(true); //this might hamper performance use hardware acc if available. see: http://developer.android.com/reference/android/view/View.html#buildDrawingCache(boolean) 

    //create the bitmaps 
    Bitmap zoomedBitmap = Bitmap.createScaledBitmap(yourImageView.getDrawingCache(true), outputSize, outputSize, true); 

    yourImageView.setDrawingCacheEnabled(false); 

現在你有了你的縮放圖像,只需要將它保存到文件。有很多關於保存到文件的信息。總之:

File myFile = new File(thePathOfYourFile); //have a look at the android api docs for File for proper explanation 
FileOutputStream fileOutputStream = null; 
    try { 
     fileOutputStream = new FileOutputStream(myFile); 
     zoomedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); 
     fileOutputStream.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
1

+0

它適合我! – 2016-09-07 05:23:00

+0

getDrawingCache()返回null。 – 2016-11-10 13:13:17

相關問題