Giter Club home page Giter Club logo

sketch's Introduction

Logo Sketch

Sketch是Android上的一个图片加载器,能够帮助开发者从本地或网络读取图片,处理后显示在页面上

sample

特点

  • 支持gif图. 集成了android-gif-drawable 1.1.7可以方便的显示gif图片,感谢koral--
  • 多种URI支持. 支持http://https://asset://content://file:///sdcard/sample.png/sdcard/sample.jpgdrawable://等7种URI
  • 二级缓存. 采用Lru算法在本地和内存中缓存图片,本地缓存默认最大容量为100M,内存缓存默认最大容量为最大可用内存的八分之一
  • 各种列表支持. 在各种列表(ListView、RecyclerView)中循环使用不错位,并且不占用setTag()方法
  • SketchImageView. 提供功能SketchImageView,只需调用display***Image()系列方法即可显示各种图片,并且支持显示下载进度,显示按下效果,点击重试等常用功能
  • 重复下载过滤. 如果两个请求的图片地址一样的话,第二个就会等待第一个下载完成之后直接使用第一个下载的图片
  • 即时取消无用请求. SketchImageView在onDetachedFromWindow()或被重复利用的时候会主动取消之前的请求
  • 自动防止加载过大Bitmap 可通过maxSize来控制加载到内存的图片的尺寸,默认为屏幕宽高的0.75倍,还会自动根据ImageView的layout_width和layout_height来调整maxSize
  • 独家TransitionDrawable支持. 独家支持任意尺寸的两张图片使用TransitionDrawable过渡显示,保证不变形
  • 只加载或只下载. 除了display()方法可以显示图片之外,你还可以通过load()方法只加载图片到内存中或通过download()方法只下载图片到本地
  • 支持读取APK图标. 支持直接读取本地APK文件的图标或根据包名和版本号读取已安装APP的图标
  • 移动网络下暂停下载. 内置了移动网络下暂停下载图片的功能,你只需开启即可
  • 自动选择合适的Bimtap.Config. 根据图片的MimeType自动选择合适的Bitmap.Config,减少内存浪费,例如对于JPEG格式的图片就会使用Bitmap.Config.RGB_565解码
  • 特殊文件预处理. 提供ImagePreprocessor,可对本地的特殊文件(例如多媒体文件)进行预处理,Sketch便可直接显示其封面,读取APK文件的图标就是通过这个功能实现的
  • 强大且灵活的自定义. 可自定义下载、缓存、解码、处理、显示、占位图等各个环节

示例APP

SampleApp

扫描二维码下载示例APP,也可点击直接下载(Click download APK)

示例

SketchImageView sketchImageView = findViewByID(R.id.image_main);

// display image from network
sketchImageView.displayImage("http://b.zol-img.com.cn/desk/bizhi/image/4/1366x768/1387347695254.jpg");

// display image from SDCard
sketchImageView.displayImage("/sdcard/sample.png");
sketchImageView.displayImage("file:///sdcard/sample.png");

// display apk icon from SDCard
sketchImageView.displayImage("/sdcard/google_play.apk");

// display installed app icon
sketchImageView.displayInstalledAppIcon("com.tencent.qq", 50001);

// display resource drawable
sketchImageView.displayResourceImage(R.drawable.sample);

// display image from asset
sketchImageView.displayAssetImage("sample.jpg");

// display image from URI
Uri uri = ...;
sketchImageView.displayURIImage(uri);

SketchImageView详细使用说明.md

使用指南

导入

从JCenter导入

dependencies{
	compile compile 'me.xiaopan:sketch:lastVersionName'
}

lastVersionNameversion

添加以下权限

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

最低支持Android2.2 API 7

支持的URI

Type Scheme Method
File in network http://, https:// displayImage(String)
File in SDCard /, file:// displayImage(String)
Content Provider content:// displayURIImage(Uri)
Asset in app asset:// displayAssetImage(String)
Resource in app resource:// displayResourceImage(int)

支持的图片类型

|Type|Scheme|jpeg|png|webp|gif|apk icon| |:---|:---|:---|:--|:---|:--|:---|:---| |File in network|http://, http:// |YES|YES|YES(Android4.0 above)|YES|NO| |File in SDCard|/, file:// |YES|YES|YES(Android4.0 above)|YES|YES| |Content Provider|content:// |YES|YES|YES(Android4.0 above)|YES|NO| |Asset in app|asset:// |YES|YES|YES(Android4.0 above)|YES|NO| |Resource in app|resource:// |YES|YES|YES(Android4.0 above)|YES|NO|

download()、load()、display()

Sketch共有display()、load()、download()三个方法可供使用,你可以根据你的需求选择合适的方法

  • download()方法会下载图片到本地,并实现本地缓存
  • load()方法在download()方法的基础上,加载图片到内存中,并对图片进行处理
  • display()方法在load()方法的基础上,将图片缓存在内存中并显示在ImageView上

示例:

// 显示
Sketch.with(context).display("http://biying.png", sketchImageView)
    .loadingImage(R.drawable.image_loading)
    .commit();

// 加载
Sketch.with(context).load("http://biying.png", new LoadListener() {
    @Override
    public void onCompleted(Bitmap bitmap, ImageFrom imageFrom, String mimeType) {

    }

    @Override
    public void onCompleted(GifDrawable gifDrawable, ImageFrom imageFrom, String mimeType) {

    }
	...
}).maxSize(100, 100).commit();

// 下载
Sketch.with(context).download("http://biying.png", new DownloadListener() {
    @Override
    public void onCompleted(File cacheFile, boolean isFromNetwork) {
        
    }
	...
}).commit();

load()和download()还支持同步执行,详情请参考同步执行load和download.md

下面是三种方法所支持的属性('-'代表不支持)

属性 download() load() display()
sync false false -
requestLevel NET NET NET
listener null null null
downloadProgressListener null null null
disableCacheInDisk false false false
maxSize - 屏幕的0.75倍 优先考虑ImageView的layout_width和layout_height
resize - null null
forceUseResize - false false
processor - null null
decodeGifImage - false false
lowQualityImage - false false
bitmapConfig - null null
inPreferQualityOverSpeed - false false
disableCacheInMemory - - false
displayer - - DefaultImageDisplayer
loadingImage - - null
failedImage - - null
pauseDownloadImage - - null
resizeByFixedSize - - false

各属性的详细说明请参考配置各种属性.md

你可能还感兴趣的功能:

增强用户体验:

其它:

Thanks

koral - android-gif-drawable

License

Copyright (C) 2013 Peng fei Pan <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

sketch's People

Watchers

Michael jentsch avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.