SimpleCropView

介绍:

一个图片裁减库,可以自定义UI,非常不错。

运行效果:

使用说明:

依赖:

repositories {
    jcenter()
}
dependencies {
    compile 'com.isseiaoki:simplecropview:1.0.7'
}

添加com.isseiaoki.simplecropview.CropImageView 到布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <com.isseiaoki.simplecropview.CropImageView
        android:id="@+id/cropImageView"
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:padding="16dp"
        custom:cropMode="ratio_1_1"
        />
    <Button
        android:id="@+id/crop_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="16dp"
        android:text="CROP"
        />
    <ImageView
        android:id="@+id/croppedImageView"
        android:layout_margin="16dp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

设置图片,获取裁减后的图片

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
        final ImageView croppedImageView = (ImageView)findViewById(R.id.croppedImageView);
        // Set image for cropping
        cropImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.sample5));
        Button cropButton = (Button)findViewById(R.id.crop_button);
        cropButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Get cropped image, and show result.
                croppedImageView.setImageBitmap(cropImageView.getCroppedBitmap());
            }
        });
    }
}

以上介绍的是基本用法,下面是更多自定义用法。

自定义

blob.pngblob.pngblob.pngblob.pngblob.pngblob.png

CropMode

裁减范围的宽高比选项

CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setCropMode(CropImageView.CropMode.RATIO_16_9);

CropMode 的值:

RATIO_4_3, RATIO_3_4, RATIO_1_1, RATIO_16_9, RATIO_9_16, RATIO_FIT_IMAGE, RATIO_FREE

RATIO_FREE: 没有固定的宽高比模式 
RATIO_X_Y: 固定的宽高比

RATIO_FIT_IMAGE: 固定的宽高比,但是比例和原始图像一致。

如果你需要其他宽高比,使用setCustomRatio(int ratioX, int ratioY);

68747470733a2f2f7261772e6769746875622e636f6d2f77696b692f4973736569416f6b692f53696d706c6543726f70566965772f696d616765732f6769662f64656d6f5f63726f705f6d6f64652e676966.gif

MinimumFrameSize

裁减区域的最小大小 单位dp

CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setMinFrameSizeInDp(100);

68747470733a2f2f7261772e6769746875622e636f6d2f77696b692f4973736569416f6b692f53696d706c6543726f70566965772f696d616765732f6769662f64656d6f5f6d696e696d756d5f6672616d655f73697a652e676966.gif

颜色

CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setBackgroundColor(0xFFFFFFFB);
cropImageView.setOverlayColor(0xAA1C1C1C);
cropImageView.setFrameColor(getResources().getColor(R.color.frame));
cropImageView.setHandleColor(getResources().getColor(R.color.handle));
cropImageView.setGuideColor(getResources().getColor(R.color.guide));

blob.png

线条与拖块的粗细

CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setFrameStrokeWeightInDp(1);
cropImageView.setGuideStrokeWeightInDp(1);
cropImageView.setHandleSizeInDp(getResources().getDimension(R.dimen.handle_size));

blob.png

触摸区域

裁减拖块的额外触摸区域。

CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setTouchPadding(16);

blob.png

拖块与辅助线条的显示模式

CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView);
cropImageView.setHandleShowMode(CropImageView.ShowMode.SHOW_ALWAYS);
cropImageView.setGuideShowMode(CropImageView.ShowMode.SHOW_ON_TOUCH);

ShowMode 值:

SHOW_ALWAYS, NOT_SHOW, SHOW_ON_TOUCH
拖块显示模式辅助线条显示模式外观
SHOW_ALWAYSSHOW_ALWAYSblob.png
NOT_SHOWNOT_SHOWblob.png
SHOW_ALWAYSNOT_SHOWblob.png
SHOW_ALWAYSSHOW_ON_TOUCH68747470733a2f2f7261772e6769746875622e636f6d2f77696b692f4973736569416f6b692f53696d706c6543726f70566965772f696d616765732f6769662f64656d6f5f67756964655f73686f775f6f6e5f746f7563682e676966.gif
SHOW_ON_TOUCHNOT_SHOW68747470733a2f2f7261772e6769746875622e636f6d2f77696b692f4973736569416f6b692f53696d706c6543726f70566965772f696d616765732f6769662f64656d6f5f68616e646c655f73686f775f6f6e5f746f7563682e676966.gif

XML 属性

xml的例子:

    <com.isseiaoki.simplecropview.CropImageView
        android:id="@+id/cropImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="32dp"
        custom:imgSrc="@mipmap/ic_launcher"
        custom:cropMode="ratio_fit_image"
        custom:minFrameSize="50dp"
        custom:backgroundColor="@color/background_material_dark"
        custom:overlayColor="#66000000"
        custom:frameColor="@android:color/white"
        custom:handleColor="@android:color/white"
        custom:guideColor="#BBFFFFFF"
        custom:frameStrokeWeight="3dp"
        custom:guideStrokeWeight="1dp"
        custom:handleSize="32dp"
        custom:touchPadding="0dp"
        custom:guideShowMode="not_show"
        custom:handleShowMode="show_always"
        custom:cropEnabled="true"
        />

| XML Attribute

(custom:)Related MethodDescription
imgSrcsetImageResource(int resId)Set source image.
cropModesetCropMode(CropImageView.CropMode mode)Set crop mode.
minFrameSizesetMinFrameSizeInDp(int minDp)Set the image cropping frame minimum size in density-independent pixels.
backgroundColorsetBackgroundColor(int bgColor)Set view background color.
overlayColorsetOverlayColor(int overlayColor)Set image overlay color.
frameColorsetFrameColor(int frameColor)Set the image cropping frame color.
handleColorsetHandleColor(int frameColor)Set the handle color.
guideColorsetGuideColor(int frameColor)Set the guide color.
handleSizesetHandleSizeInDp(int handleDp)Set handle radius in density-independent pixels.
touchPaddingsetTouchPaddingInDp(int paddingDp)Set the image cropping frame handle touch padding(touch area) in density-independent pixels.
frameStrokeWeightsetFrameStrokeWeightInDp(int weightDp)Set frame stroke weight in density-independent pixels.
guideStrokeWeightsetGuideStrokeWeightInDp(int weightDp)Set guideline stroke weight in density-independent pixels.
guideShowModesetGuideShowMode(CropImageView.ShowMode mode)Set guideline show mode.
handleShowModesetHandleShowMode(CropImageView.ShowMode mode)Set handle show mode.
cropEnabledsetCropEnabled(boolean enabled)Set whether to show the image cropping frame.
已下载
0