用開源項目cropper實現對圖片中任意部分進行裁剪


 紅色區域為截圖控件的區域。   

  

  開源項目地址:https://github.com/edmodo/cropper

  croper這個開源項目可以對一個圖片進行任意區域的街區,並且可以設置圖片的旋轉角度。但它忽視了小圖片的存在,如果要截圖的圖片過於小,那么顯示效果是極其不好的。於是我寫了個圖片拉伸的方法來解決這個問題,當然也可以再它的源碼中進行修改。我嘗試過在源碼中修改,但發現它多方法都是針對的大圖來做的,修改起來十分困難。所以姑且偷懶先把小圖片拉伸后在進行顯示。

使用的步驟很簡單:

1.在布局文件定義這個控件

2.在代碼中找到他,並進行下設置(可選)

3.使用截圖的方法

 

布局文件(里面的imageview用於顯示截取好的圖片,button是用來截圖的按鈕)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <com.edmodo.cropper.CropImageView
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/CropImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:background="#ff0000"/>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/button1"
            android:layout_centerHorizontal="true"
            android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="裁剪" />

</RelativeLayout>

 

MainActivity

package com.kale.croppertest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import com.edmodo.cropper.CropImageView;

public class MainActivity extends Activity {
    CropImageView cImageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cImageView = (CropImageView) findViewById(R.id.CropImageView);
        cImageView.setImageBitmap(getBitmap(R.drawable.right));//為了兼容小圖片,必須在代碼中加載圖片
        cImageView.rotateImage(30);//設定圖片的旋轉角度
        cImageView.setFixedAspectRatio(true);//設置允許按比例截圖,如果不設置就是默認的任意大小截圖
        cImageView.setAspectRatio(1, 1);//設置比例為一比一
        cImageView.setGuidelines(CropImageView.ON);//設置顯示網格的時機,默認為on_touch
        
        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO 自動生成的方法存根
                Bitmap bitmap = cImageView.getCroppedImage();//得到裁剪好的圖片
                ImageView croppedImageView = (ImageView) findViewById(R.id.imageView);
                croppedImageView.setImageBitmap(bitmap);//設置到imageview中
            }
        });
    }
    
    /**
     * @param resId
     * @return 如果圖片太小,那么就拉伸
     */
    public Bitmap getBitmap(int resId) {
        WindowManager wm = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(), resId);
        float scaleWidth = 1,scaleHeight = 1;
        if(bitmap.getWidth() < width) {
            scaleWidth = width / bitmap.getWidth();
            scaleHeight = scaleWidth;
        }
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight()
                , matrix, true);
        return bitmap;
    }

}

 

源碼下載:http://download.csdn.net/detail/shark0017/7732283

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM