ASampleCache缓存Drawable,Bitmap

缓存用的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);
            }
        });

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值