Android 图片功能模块相关库

uCrop


uCrop - Image Cropping Library for Android

This project aims to provide an ultimate and flexible image cropping experience. Made in [Yalantis] (https://yalantis.com/?utm_source=github)

Usage

  1. Include the library as local library project. compile 'com.yalantis:ucrop:2.1.1'
  2. Add UCropActivity into your AndroidManifest.xml <activity android:name="com.yalantis.ucrop.UCropActivity" android:screenOrientation="portrait" android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
  3. The uCrop configuration is created using the builder pattern. UCrop.of(sourceUri, destinationUri) .withAspectRatio(16, 9) .withMaxResultSize(maxWidth, maxHeight) .start(context);
  4. Override onActivityResult method and handle uCrop result. @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) { final Uri resultUri = UCrop.getOutput(data); } else if (resultCode == UCrop.RESULT_ERROR) { final Throwable cropError = UCrop.getError(data); } }
  5. You may want to add this to your PROGUARD config: -dontwarn com.yalantis.ucrop** -keep class com.yalantis.ucrop** { *; } -keep interface com.yalantis.ucrop** { *; }

Customization

If you want to let your users choose crop ratio dynamically, just do not call withAspectRatio(x, y).

uCrop builder class has method withOptions(UCrop.Options options) which extends library configurations.

Currently you can change:

  • image compression format (e.g. PNG, JPEG, WEBP), compression
  • image compression quality [0 - 100]. PNG which is lossless, will ignore the quality setting.
  • whether all gestures are enabled simultaneously
  • maximum size for Bitmap that is decoded from source Uri and used within crop view. If you want to override default behaviour.
  • toggle whether to show crop frame/guidelines
  • setup color/width/count of crop frame/rows/columns
  • choose whether you want rectangle or oval crop area
  • the UI colors (Toolbar, StatusBar, active widget state)
  • and more…

Compatibility

  • Library - Android ICS 4.0+ (API 14) (Android GINGERBREAD 2.3+ (API 10) for versions <= 1.3.2)
  • Sample - Android ICS 4.0+ (API 14)
  • CPU - armeabi-v7a x86 x86_64 arm64-v8a (for versions >= 2.1.0)

PhotoView

PhotoView aims to help produce an easily usable implementation of a zooming Android ImageView.

Features

  • Out of the box zooming, using multi-touch and double-tap.
  • Scrolling, with smooth scrolling fling.
  • Works perfectly when used in a scrolling parent (such as ViewPager).
  • Allows the application to be notified when the displayed Matrix has changed. Useful for when you need to update your UI based on the current zoom/scroll position.
  • Allows the application to be notified when the user taps on the Photo.

Gradle Dependency

Add this in your root build.gradle file (not your module build.gradle file):

allprojects {
	repositories {
		...
		maven { url "https://jitpack.io" }
	}
}

Then, add the library to your project build.gradle

dependencies {
    compile 'com.github.chrisbanes:PhotoView:1.2.6'
}

Sample Usage

There is a sample provided which shows how to use the library in a more advanced way, but for completeness here is all that is required to get PhotoView working:

ImageView mImageView;
PhotoViewAttacher mAttacher;

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);

	// Any implementation of ImageView can be used!
	mImageView = (ImageView) findViewById(R.id.iv_photo);

	// Set the Drawable displayed
	Drawable bitmap = getResources().getDrawable(R.drawable.wallpaper);
	mImageView.setImageDrawable(bitmap);

	// Attach a PhotoViewAttacher, which takes care of all of the zooming functionality.
	// (not needed unless you are going to change the drawable later)
	mAttacher = new PhotoViewAttacher(mImageView);
}

// If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call
mAttacher.update();

Issues With ViewGroups

There are some ViewGroups (ones that utilize onInterceptTouchEvent) that throw exceptions when a PhotoView is placed within them, most notably ViewPager and DrawerLayout. This is a framework issue that has not been resolved. In order to prevent this exception (which typically occurs when you zoom out), take a look at HackyDrawerLayout and you can see the solution is to simply catch the exception. Any ViewGroup which uses onInterceptTouchEvent will also need to be extended and exceptions caught. Use the HackyDrawerLayout as a template of how to do so. The basic implementation is:

public class HackyProblematicViewGroup extends ProblematicViewGroup {

    public HackyProblematicViewGroup(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            return super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException e) {
						//uncomment if you really want to see these errors
            //e.printStackTrace();
            return false;
        }
    }
}

Usage with Fresco

Due to the complex nature of Fresco, this library does not currently support Fresco. See this project as an alternative solution.

Subsampling Support

This library aims to keep the zooming implementation simple. If you are looking for an implementation that supports subsampling, check out this project


AndroidGradientImageView

AndroidGradientImageView is a simple imageview which overlays gradient on its content like below:

Setup Gradle

dependencies {
    ...
    compile 'kr.pe.burt.android.lib:androidgradientimageview:0.0.2'
}

Attributes

AndroidGradientImageView is a simple imageview. It just provides a few attributes for gradient effect.

  • giv_x * is left position of gradient * is float value * you can set value from 0.0 to 1.0
  • giv_y
    • is top position of gradient * is float value * you can set value from 0.0 to 1.0
  • giv_width
    • is gradient’s width * is float value * you can set value from 0.0 to 1.0
  • giv_height
    • is gradient’s height * is float value * you can set value from 0.0 to 1.0
  • giv_rotate
    • is rotation degree of gradient * is float value * you can set value from 0.0 to 360.0
  • giv_startColor
    • is start color of gradient * is color value * you can set value as #FFFFFF or @color/what_color
  • giv_endColor
    • is end color of gradient * is color value * you can set value as #FFFFFF or @color/what_color
  • giv_middleColor
    • is middle color of gradient * is color value * you can set value as #FFFFFF or @color/what_color
  • giv_startOffset
    • is offset of start color of gradient * is float value * you can set value from 0.0 to 1.0
  • giv_middleOffset
    • is offset of middle color of gradient * is float value * you can set value from 0.0 to 1.0
  • giv_endOffset
    • is offset of end color of gradient * is float value * you can set value from 0.0 to 1.0
  • giv_alpha
    • is alpha of the gradient
    • is float value
    • you can set value from 0.0 to 1.0

Examples

<kr.pe.burt.android.lib.androidgradientimageview.AndroidGradientImageView
        android:src="@mipmap/ic_launcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        app:giv_x="0.0"
        app:giv_y="0.0"
        app:giv_rotate="45.0"
        app:giv_startColor="#8F00"
        app:giv_startOffset="0.3"
        app:giv_middleColor="#80F0"
        app:giv_middleOffset="0.6"
        app:giv_endColor="#800F"
        app:giv_endOffset="0.8"
        />

or you can set attributes just you need

<kr.pe.burt.android.lib.androidgradientimageview.AndroidGradientImageView
        android:src="@drawable/agassi"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        app:giv_rotate="45.0"
        app:giv_startOffset="0.3"
        app:giv_endColor="#c000"
        app:giv_endOffset="0.6"
        />

android-image-picker

android 图片多选控件

Gradle

repositories {
    maven { url "https://jitpack.io" }
}
dependencies {
    compile 'com.github.sd6352051:android-image-picker:v1.0.1'
}

如何使用

Step 1: 让你的activity 继承 BaseGalleryActivity

public class YourActivity extends BaseGalleryActivity{

}

Step 2: 添加控件的根布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/gallery_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="48dp"/>
</RelativeLayout>

Step 3: 在你的activity中调用此 attachFragment(R.id.gallery_root)

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        attachFragment(R.id.gallery_root);
    }

Step 4: 实现接口

  @Override
    public Configuration getConfiguration() {
        //返回控件配置
        Configuration cfg=new Configuration.Builder()
                .hasCamera(true)
                .hasShade(true)
                .hasPreview(true)
                .setSpaceSize(3)
                .setPhotoMaxWidth(120)
                .setCheckBoxColor(0xFF3F51B5)
                .setDialogHeight(Configuration.DIALOG_HALF)
                .setDialogMode(Configuration.DIALOG_GRID)
                .setMaximum(9)
                .setTip(null)
                .setAblumsTitle(null)
                .build();
        return cfg;
    }

    @Override
    public List<PhotoEntry> getSelectPhotos() {
        //返回当前已经选中的图片 没有是返回null
        return mSelectedPhotos;
    }

    @Override
    public void onSelectedCountChanged(int count) {
        //这个方法将在你图选择发生变化时调用
        // count:当前被选中图像数量
    }

    @Override
    public void onAlbumChanged(String name) {
        //这个方法将在相册选择发生变化时调用
        //name:当前选中的相册名称
    }

    @Override
    public void onTakePhoto(PhotoEntry entry) {
        //这个方法将在你拍照后调用
        //entry:返回拍照后的图片信息
    }

    @Override
    public void onChoosePhotos(List<PhotoEntry> entries) {
        //这个方法将在你调用 sendPhotos() 方法后调用
        //entries:返回你选中的图片信息
    }

    @Override
    public void onPhotoClick(PhotoEntry entry) {
        //这个方法将在你点击图像时调用,如果configuration hasPreview(false)时 将不回调此方法
        //entry: 返回当前点击的图像信息
    }

Step 5: 其他方法

openAlbum(): 打开相册弹窗

sendPhotos(): 这个方法将会被调用 onChoosePhotos(List<PhotoEntry> entries)

配置项

默认配置

Configuration cfg=new Configuration.Builder()
                //是否包含相机按钮
                .hasCamera(true)
                //图片选中后是否包含一个浮层
                .hasShade(true)
                //是否包含图像预览功能,为false时点击图像将执行反选效果,true则会调用onPhotoClick方法
                .hasPreview(true)
                //GridView间隔
                .setSpaceSize(4)
                //图像最大宽度
                .setPhotoMaxWidth(120)
                //Checkbox 背景色
                .setCheckBoxColor(0xFF3F51B5)
                //相册弹出默认高度
                .setDialogHeight(Configuration.DIALOG_HALF)
                //相册模式  DIALOG_GRID  网格形式  DIALOG_LIST 列表形式
                .setDialogMode(Configuration.DIALOG_GRID)
                //图像最大选中
                .setMaximum(9)
                //图像达到最大选中时的提示
                .setTip(null)
                //相册标题
                .setAblumsTitle(null)
                .build();

Compressor

Compressor is a lightweight and powerful android image compression library. Compressor will allow you to compress large photos into smaller sized photos with very less or negligible loss in quality of the image.

Gradle

dependencies {
    compile 'id.zelory:compressor:1.0.2'
}

Let’s compress the image size!

Compress Image File

compressedImageFile = Compressor.getDefault(this).compressToFile(actualImageFile);

Compress Image File to Bitmap

compressedImageBitmap = Compressor.getDefault(this).compressToBitmap(actualImageFile);

I want custom Compressor!

compressedImage = new Compressor.Builder(this)
            .setMaxWidth(640)
            .setMaxHeight(480)
            .setQuality(75)
            .setCompressFormat(Bitmap.CompressFormat.WEBP)
            .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES).getAbsolutePath())
            .build()
            .compressToFile(actualImage);

Stay cool compress image asynchronously with RxJava!

Compressor.getDefault(this)
        .compressToFileAsObservable(actualImage)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Action1<File>() {
            @Override
            public void call(File file) {
                compressedImage = file;
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                showError(throwable.getMessage());
            }
        });

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏developerHaoz 的安卓之旅

手把手教你从零开始做一个好看的 APP - Day four

本文为 手把手教你从零开始做一个好看的 APP - Day four ,如果想看该系列的其他文章,请点击以下连接

912
来自专栏java初学

android入门 — ProgressDialog/DatePickerDialog/TimePickerDialog

29411
来自专栏向治洪

android自定义状态栏颜色

我们知道IOS上的应用,状态栏的颜色总能与应用标题栏颜色保持一致,用户体验很不错,那安卓是否可以呢?若是在安卓4.4之前,答案是否定的,但在4.4之后,谷歌允...

3276
来自专栏向治洪

listview的工作原理

/**      * Unsorted views that can be used by the adapter as a convert vi...

22310
来自专栏Android中高级开发

深入分析Android动画(二)

上回书说到Android动画的分类以及基本使用,这会书主要说Android属性动画的原理,对于View动画的原理本篇不做深入分析。对于Android动画的基础请...

962
来自专栏向治洪

android来电归属地提醒

现在市面上常用的一些拨号软件的一个功能,来电归属地。拨号的时候,会在拨号界面出现一个号码归属地的小框框。效果如下:而且这个小窗体还可以自定义风格,并且可以自由移...

2577
来自专栏项勇

笔记69 | Android获取MP3文件的ID3信息工具类

1785
来自专栏浅探ARKit

ARKit同时检测水平平面和竖直平面

ARKit1.5里,新增了检测竖直平面的功能。为此特意写一个demo。 下面是效果图: [IMG_3728.PNG] 和之前的水平平面相比 其实就是把多一个属性...

42310
来自专栏Samego开发资源

图片自动轮播图

4066
来自专栏我就是马云飞

我奶奶都能懂的UI绘制流程(上)

前言 从今天开始,慢慢整理Android高级UI的知识,涉及到各种酷炫狂拽吊炸天的特效。 之前写过一篇Window一本满足算是这个专题的预备知识,本文就基于这篇...

2206

扫码关注云+社区

领取腾讯云代金券