Android ImageSwitcher 图片切换 按钮点击切换

图片显示:

 

1. MainActivity

package com.gamedog.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity implements ViewFactory
{

	private ImageSwitcher switcher;
	private Button forward;
	private Button next;
	// 图片索引
	private static int index = 0;
	// 显示的图片资源

	private static final Integer[] imagelist =
	{ R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6 };

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		forward = (Button) findViewById(R.id.forward);
		next = (Button) findViewById(R.id.next);
		switcher = (ImageSwitcher) findViewById(R.id.image);
		switcher.setFactory(this);
		switcher.setImageResource(imagelist[index]);

		// 上一张
		forward.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				index--;
				if (index < 0)
				{
					index = imagelist.length - 1;
				}
				switcher.setImageResource(imagelist[index]);
			}
		});
		// 下一张
		next.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				index++;
				if (index >= imagelist.length)
				{
					index = 0;
				}
				switcher.setImageResource(imagelist[index]);
			}
		});
	}

	// 用于显示图片
	@Override
	public View makeView()
	{
		return new ImageView(this);
	}
}


2.布局文件

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
            android:orientation="vertical" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
                  android:orientation="horizontal" 
                  android:layout_width="fill_parent" 
                  android:layout_height="fill_parent" 
                  android:gravity="center"
                  android:background="#f0f0f0"
            > 
        <Button 
                android:id="@+id/forward" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:background="@drawable/left_btn"
                android:layout_gravity="center"
                /> 
        <ImageSwitcher 
                android:id="@+id/image" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                /> 
        <Button 
                android:id="@+id/next" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:background="@drawable/right_btn"
                 android:layout_gravity="center"
                /> 
 
    </LinearLayout> 
</ScrollView> 


3.网络取图片时候调整图片的固定大小

public static Drawable resizeImage(Bitmap bitmap, int w, int h)
	{

		Bitmap BitmapOrg = bitmap;

		int width = BitmapOrg.getWidth();
		int height = BitmapOrg.getHeight();
		int newWidth = w;
		int newHeight = h;

		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;

		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);

		Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);
		
		return new BitmapDrawable(resizedBitmap);

	}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值