一、Picasso介绍
picasso是Square公司开源的一个Android第三方图形缓存库,地址http://square.github.io/picasso/,可以实现图片下载和缓存功能。
二、Picasso基本用法
Picasso.with(mContext).load(imageUrl).into(imageView);
代码很简洁。这一行代码解决了一下问题:
1.自动将图像缓存在本地
2.通过图片压缩转换以减少内存消耗
3.自动处理了ImageView的回收,即自动取消不在视野范围内的ImageView视图资源的加载
三、Picasso其他用法
1)在Adapter中使用
@Override
public void getView(int position, View convertView, ViewGroup parent) {
SquaredImageView view = (SquaredImageView) convertView;
if (view == null) {
view = new SquaredImageView(mContext);
}
String url = getItem(position);
Picasso.with(mContext).load(url).into(view);
}
2)图片转换
1
Picasso.with(mContext).load(imageUrl).resize(200,200).centerCrop().into(imageView);
还可以自定义转换:
Transformation transformation = new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
int targetWidth = getWidth();
double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
int targetHeight = (int) (targetWidth * aspectRatio);
Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
if (result != source) {
//如果大小是相同的,相同的位图就会被返回
source.recycle();
}
return result;
}
@Override
public String key() {
return "transformation" + " desiredWidth";
}
};
定义好Transformation后,在Picasso设置transform
Picasso.with(mContext).load(imageUrl).transform(transformation).into(viewHolder.mImageView);
或者通过实现Transformation接口自定义
public class CropSquareTransformation implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap result = Bitmap.createBitmap(source, x, y, size, size);
if (result != source) {
source.recycle();
}
return result;
}
@Override
public String key() {
return "square()";
}
}
然后将CropSquareTransformation的对象传递给transform方法即可。
3)图片未正常显示时默认的图片,通过placeholder()设置;图片显示错误时显示的默认图片,通过error()设置
Picasso.with(mContext)
.load(url)
.placeholder(R.mipmap.icon_loading)
.error(R.mipmap.icon_loading)
.into(imageView);
4)加载Resources, assets, files, content providers中的资源文件
Picasso.with(mContext).load(R.mipmap.icon).into(imageView);
Picasso.wiht(mContext).load(new File(...)).into(imageView);
四、内存优化
1)查看大图放弃memory cache
Picasso默认会使用设备的15%的内存作为内存图片缓存,且现有的api无法清空内存缓存。我们可以在查看大图时放弃使用内存缓存,图片从网络下载完成后会缓存到磁盘中,加载会从磁盘中加载,这样可以加速内存的回收。
Picasso.with(mContext)
.load(imageUrl)
.memoryPolicy(NO_CACHE, NO_STORE)
.into(imageView);
其中memoryPolicy的NO_CACHE是指图片加载时放弃在内存缓存中查找,NO_STORE是指图片加载完不缓存在内存中。
2)列表页滑动优化
picasso可以对多个加载请求设置相同的tag,即
Object tag = new Object();
Picasso.with(mContext)
.load(url)
.resize(200,200)
.centerCrop()
.tag(tag)
.into(imageView);
例如在RecyclerView滑动时监听,处理不同的表现:
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState){
if (newState == RecyclerView.SCROLL_STATE_IDLE){
Picasso.with(mContext).resumeTag(tag);
}else{
Picasso.with(mContext).pauseTag(tag);
}
});
一、加载Html代码
String html="<a href=\"http://www.111cn.net \"> 百度</a>;";
textView.setMovementMethod(LinkMovementMethod.getInstance());//允许超链接点击跳转到浏览器
textView.setText(Html.fromHtml(html));
二、加载src标签
1、加载本地图片
String html="<img" width=100% src=\"file:///system/media/Pre-loaded/Pictures/Picture_01_Greenery.jpg\" />";
textView.setText(Html.fromHtml(html, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = null;
try {
drawable = Drawable.createFromStream(new URL(source).openStream(), null);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
} catch (IOException e) {
e.printStackTrace();
}
return drawable;
}
},null));
注意:file:/别忘了加
Html还有个三参数的fronHtml方法,三个参数分别是:String source(图片资源的路径),ImageGetter imageGetter(实现ImageGetter类的对象),TagHandler tagHandler(暂不知什么用,不过这里设为null就行了)
ImageGetter类是Html类的一个静态接口,有个方法getDrawable,返回值为Drawable,作用是取得图片的Drawable
drawable = Drawable.createFromStream(new URL(source).openStream(), null);
这句代码,有一点要说,不要用网上许多人用的:
drawable= Drawable.createFromPath(source);
可能会报FileNotFound错误。
2、加载网络图片
html代码和之前差不多,把路径换成网络图片的地址就行
比如:
String html="<img" width=100% src=\"http://img0.bdstatic.com/img/image/shouye/xiaoxiao/%E5%AE%A0%E7%89%A9622.jpg\" />";
但是ImageGetter的getDrawable方法需要变化:
这里使用Piccsso框架加载网络图片,不会的同学可以去百度一下
private class MyImageGetter implements Html.ImageGetter {
private URLDrawable urlDrawable = null;
@Override
public Drawable getDrawable(final String source) {
urlDrawable = new URLDrawable();
Picasso.with(context).load(source).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
urlDrawable.bitmap = bitmap;
urlDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
textView.setText(textView.getText());//不加这句显示不出来图片,原因不详
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
return urlDrawable;
}
}
URLDrawable类:
/**
* Created by Administrator on 2016/6/28 0028.
*/
public class URLDrawable extends BitmapDrawable {
public Bitmap bitmap;
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (bitmap != null) {
canvas.drawBitmap(bitmap, 0, 0, getPaint());
}
}
}
调用方法如下:
textView.setText(Html.fromHtml(html, new MyImageGetter(), null));
TextView间距对于各位朋友来讲是非常的重要的一个东西了,今天我们来看一篇关于Android中设置TextView的行间距的例子,具体的如下所示。一段文字,如果行间距很小的话看着就会很不舒服,我们在使用word文档的时候行间距是可以自己调整的,那么在Android开发的过程中如果一大段很长的文字如何设置行间距呢,很简单只需要一行代码:
android:lineSpacingExtra="3dp"
分享三张效果图:
第一张,没有设置行间距的,使用系统默认的效果:
第三张,设置为10dp的效果,更明显:
在使用QQ的过程中我们会发现一个很短的界面也是可以上下拖动的,这就是带有阻尼效果功能实现,要实现这样的功能呢,系统的ScrollView是没法实现的,必须要自定义继承于ScrollView的控件,先来看下效果图。
先来看看布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<frame.zmit.cn.stretchscrollview.StretchScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:background="#FFFFFF"
android:gravity="center"
android:text="5" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#E6E6E6" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:background="#FFFFFF"
android:gravity="center"
android:text="5" />
</LinearLayout>
</frame.zmit.cn.stretchscrollview.StretchScrollView>
</LinearLayout>
Activity啥也没有,就不贴了
下面贴上自定义的StretchScrollView
package frame.zmit.cn.stretchscrollview;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
/**
* A ScrollView which can scroll to (0,0) when pull down or up.
*
* @author markmjw
* @date 2014-04-30
*/
public class StretchScrollView extends ScrollView {
private static final int MSG_REST_POSITION = 0x01;
/** The max scroll height. */
private static final int MAX_SCROLL_HEIGHT = 600;
/** Damping, the smaller the greater the resistance */
private static final float SCROLL_RATIO = 0.4f;
private View mChildRootView;
private float mTouchY;
private boolean mTouchStop = false;
private int mScrollY = 0;
private int mScrollDy = 0;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (MSG_REST_POSITION == msg.what) {
if (mScrollY != 0 && mTouchStop) {
mScrollY -= mScrollDy;
if ((mScrollDy < 0 && mScrollY > 0) || (mScrollDy > 0 && mScrollY < 0)) {
mScrollY = 0;
}
mChildRootView.scrollTo(0, mScrollY);
// continue scroll after 20ms
sendEmptyMessageDelayed(MSG_REST_POSITION, 20);
}
}
}
};
public StretchScrollView(Context context) {
super(context);
init();
}
public StretchScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public StretchScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
// set scroll mode
setOverScrollMode(OVER_SCROLL_NEVER);
}
@Override
protected void onFinishInflate() {
if (getChildCount() > 0) {
// when finished inflating from layout xml, get the first child view
mChildRootView = getChildAt(0);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
mTouchY = ev.getY();
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (null != mChildRootView) {
doTouchEvent(ev);
}
return super.onTouchEvent(ev);
}
private void doTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_UP:
mScrollY = mChildRootView.getScrollY();
if (mScrollY != 0) {
mTouchStop = true;
mScrollDy = (int) (mScrollY / 10.0f);
mHandler.sendEmptyMessage(MSG_REST_POSITION);
}
break;
case MotionEvent.ACTION_MOVE:
float nowY = ev.getY();
int deltaY = (int) (mTouchY - nowY);
mTouchY = nowY;
if (isNeedMove()) {
int offset = mChildRootView.getScrollY();
if (offset < MAX_SCROLL_HEIGHT && offset > -MAX_SCROLL_HEIGHT) {
mChildRootView.scrollBy(0, (int) (deltaY * SCROLL_RATIO));
mTouchStop = false;
}
}
break;
default:
break;
}
}
private boolean isNeedMove() {
int viewHeight = mChildRootView.getMeasuredHeight();
int scrollHeight = getHeight();
int offset = viewHeight - scrollHeight;
int scrollY = getScrollY();
return scrollY == 0 || scrollY == offset;
}
}
Acache类下载地址:http://pan.baidu.com/s/1gfI0A9X
1、创建Acache实例
Acache acache;
acache = Acache.get(this, “main”);
2、将数据保存起来(内存和文件)
acache.put(key,value,time);
前两个无需赘述,键值对,最后一个time,传入int类型,单位为秒,代表缓存存在的时间,超出这个时间,就会清除掉缓存的数据。
3、获取缓存的数据
以一个例子来描述这个问题:
代码如下 | 复制代码 |
public class MainActivity extends Activity { |
当我第一次进入的时候,控制台打印如下:
当我立即退出又进去,打印如下:
当我延迟了十秒左右再进去,又提示如下:
这样可以证明缓存可用,并且缓存的时间也有效果。