图片显示:
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);
}