缓存用的ASampleCache
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.axnet.cacheimg.MainActivity">
<Button
android:id="@+id/bt1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="save"
/>
<Button
android:id="@+id/bt2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="show"
/>
<ImageView
android:id="@+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
bitmap
public class MainActivity extends Activity {
ACache mCache;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.iv1);
Button bt1=(Button)findViewById(R.id.bt1);
Button bt2=(Button)findViewById(R.id.bt2);
mCache = ACache.get(this);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.mipmap.temp);
mCache.put("testBitmap", bitmap);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap testBitmap = mCache.getAsBitmap("testBitmap");
if (testBitmap == null) {
Toast.makeText(MainActivity.this, "Bitmap cache is null ...", Toast.LENGTH_SHORT)
.show();
imageView.setImageBitmap(null);
return;
}
imageView.setImageBitmap(testBitmap);
}
});
}
}
drawable
package com.axnet.cacheimg;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
ACache mCache;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.iv1);
Button bt1=(Button)findViewById(R.id.bt1);
Button bt2=(Button)findViewById(R.id.bt2);
mCache = ACache.get(this);
/**
* ASampleCache保存drawable
*/
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Resources res = getResources();
Drawable drawable = res.getDrawable(R.mipmap.night2);
mCache.put("testDrawable", drawable);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Drawable testDrawable = mCache.getAsDrawable("testDrawable");
if (testDrawable == null) {
Toast.makeText(MainActivity.this, "Drawable cache is null ...",
Toast.LENGTH_SHORT).show();
imageView.setImageDrawable(null);
return;
}
imageView.setImageDrawable(testDrawable);
}
});
}
}