Bitmap使用

写了个小例子,读取asset目录下的图片文件。通过点击Button,显示在ImageView上。

Asset目录截图:


主Activity  code:

package com.cb.bitmaptest;

import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button mSwicthButton;
	private ImageView mImageView;
	
	private AssetManager mAssetManager;
	private String[] images = null;
	private int mCurrentImageIndex = 0;
	//若路径中没有图片文件,count统计文件数量,作为跳出循环的判断条件
	private int count = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mImageView = (ImageView)findViewById(R.id.imageview);
		//返回一个AssetManager对象,可以通过它访问asset目录
		mAssetManager = getAssets();
		try {
			//获取asset目录中的文件名
			images = mAssetManager.list("");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		mSwicthButton = (Button)findViewById(R.id.switch_picture);
		mSwicthButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//计数,若已遍历了(文件数量)次,则退出
				count = 0;
				
				//判断文件是否是图片文件,若不是,则继续往下找,直到找到下一个图片文件
				while (!isPicture()	&& (count != images.length)) {
					mCurrentImageIndex++;
					count++;
					if (mCurrentImageIndex >= images.length) {
						mCurrentImageIndex = 0;
					}
				}
				if (count == images.length) {
					Toast.makeText(MainActivity.this, R.string.no_picture, Toast.LENGTH_SHORT).show();
					return ;
				}
				
				InputStream stream = null;
				try {
					//获取文件流对象
					stream = mAssetManager.open(images[mCurrentImageIndex++]);
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				//获得drawable对象.该drawable就是ImageView的android:src属性的值,可设可不设。
				BitmapDrawable drawable = (BitmapDrawable) mImageView.getDrawable();
				//ImageView控件上的bitmap若未回收,强制回收
				if (drawable != null && !drawable.getBitmap().isRecycled()) {
					drawable.getBitmap().recycle();
				}
				
				//ImageView更改显示图片
				mImageView.setImageBitmap(BitmapFactory.decodeStream(stream));
			}
		});
		
	}

	public boolean isPicture() {
		if ((!images[mCurrentImageIndex].endsWith(".png"))						
				&&(!images[mCurrentImageIndex].endsWith(".jpg"))
				&&(!images[mCurrentImageIndex].endsWith(".gif"))) {
			return false;
		}
		return true;
	}

}

activity_main.xml 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/switch_picture"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/switchPicture" />

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redis 的 Bitmap 类型是一种特殊的字符串类型,它可以用来处理一些基于二进制位的数据结构,例如布隆过滤器(Bloom Filter)或者统计用户在线时长等。 Bitmap 类型支持的操作包括: - bitset key offset value:将指定偏移量上的二进制位设置为 1 或 0。 - bitget key offset:获取指定偏移量上的二进制位的值。 - bitcount key [start end]:计算指定范围内的二进制位被设置为 1 的个数。 - bitop operation destkey key [key ...]:对多个 Bitmap 进行按位操作,并将结果保存到目标 Bitmap 中。 - bitpos key bit [start] [end]:在指定范围内查找第一个被设置为指定值的二进制位的位置。 以下是一个示例,演示如何使用 Bitmap 类型来统计用户在线时长: ``` # 将用户的登录时间记录到 Bitmap 中 # 假设用户 ID 为 1001,登录时间为 2021-10-01 10:00:00,登录时长为 30 分钟 # 将 2021-10-01 10:00:00 转换为时间戳,并除以 300,得到登录时间所在的 5 分钟时间段 redis> setbit online:1001 200 1 # 查询用户在线时长 # 统计 2021-10-01 10:00:00 ~ 2021-10-01 10:30:00 时间段内,用户在线的 5 分钟时间段个数 redis> bitcount online:1001 0 -1 6 ``` 在上面的示例中,我们将用户的登录时间记录到 online:1001 这个 Bitmap 中,偏移量为 200 的二进制位被设置为 1。接着,我们使用 bitcount 命令来统计该 Bitmap 中被设置为 1 的二进制位数量,即用户在线的时间段个数。由于每个时间段为 5 分钟,因此在线时长为 30 分钟,对应的时间段个数为 6。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值