这次主要介绍Android4.0图片转换的功能,具体运行效果如下:
要实现这个效果主要用到两个控件ImageView和Gallery,具体的界面布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/picGallery"
android:src="@drawable/girl_1"/>
<Gallery
android:id="@+id/picGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
新建values\attrs.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- These are the attributes that we want to retrieve from the theme
in view/Gallery1.java -->
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
具体的实现JAVA代码如下:
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Gallery picGallery;
private ImageView myImageView;
private Integer[] images = {
R.drawable.girl_1,
R.drawable.girl_2,
R.drawable.girl_3,
R.drawable.girl_4,
R.drawable.girl_5,
R.drawable.girl_6,
R.drawable.girl_7,
R.drawable.girl_8,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
picGallery = (Gallery) findViewById(R.id.picGallery);
myImageView = (ImageView) findViewById(R.id.myImageView);
picGallery.setAdapter(new ImageAdapter(this));
picGallery.setOnItemClickListener(listener);
}
private OnItemClickListener listener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
Drawable drawable = MainActivity.this.getResources().getDrawable(images[position]);
myImageView.setImageDrawable(drawable);
}
};
public class ImageAdapter extends BaseAdapter {
private static final int ITEM_WIDTH = 136;
private static final int ITEM_HEIGHT = 88;
private final int mGalleryItemBackground;
private final Context mContext;
private final Integer[] mImageIds = {
R.drawable.girl_1,
R.drawable.girl_2,
R.drawable.girl_3,
R.drawable.girl_4,
R.drawable.girl_5,
R.drawable.girl_6,
R.drawable.girl_7,
R.drawable.girl_8,
};
private final float mDensity;
public ImageAdapter(Context c) {
mContext = c;
// See res/values/attrs.xml for the <declare-styleable> that defines
// Gallery1.
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
mDensity = c.getResources().getDisplayMetrics().density;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
convertView = new ImageView(mContext);
imageView = (ImageView) convertView;
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(
(int) (ITEM_WIDTH * mDensity + 0.5f),
(int) (ITEM_HEIGHT * mDensity + 0.5f)));
// The preferred Gallery item background
imageView.setBackgroundResource(mGalleryItemBackground);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mImageIds[position]);
return imageView;
}
}
}