android gallery和Animation组合使用, 看美女美图
今天主要探究Gallery和Animation的使用。 制作一个美女图片集锦。1. 首先需要做一个列表, 用来显示类别:
public class CategoryActivity extends Activity implements OnItemClickListener{private ListView mList;LayoutInflater mInflater;private Integer[] title = {R.string.jingxuan,R.string.mingxing,R.string.xinggan,R.string.cosplay,R.string.socks,R.string.more};@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.category_list); mList = (ListView) findViewById(R.id.category_list); long time = System.currentTimeMillis(); if (time%3==0) { mList.setBackgroundResource(R.drawable.longze2); } else if (time%3==1){ mList.setBackgroundResource(R.drawable.pic02); } else { mList.setBackgroundResource(R.drawable.pic05); } mInflater = LayoutInflater.from(this); mList.setAdapter(new TitleAdapter(this)); mList.setOnItemClickListener(this); }
...
需要自定义个一个TitleAdapter集成BaseAdapter,
private class TitleAdapter extends BaseAdapter { private Context mContext; public TitleAdapter(Context context) { mContext = context; }@Overridepublic int getCount() {// TODO Auto-generated method stubreturn title.length;}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn title;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder vh = null;if (convertView == null) {convertView = mInflater.inflate(R.layout.category_item, null);vh = new ViewHolder();vh.titleView = (TextView) convertView.findViewById(R.id.cate_title);//hilight the last 'more' titleif (position == title.length -1) {vh.titleView.setTextColor(CategoryActivity.this.getResources().getColor(R.color.red));}convertView.setTag(vh);} else {vh = (ViewHolder) convertView.getTag();}//set image resourcevh.titleView.setText(title);return convertView;}/** * holder for caching UI component * @author hp * */class ViewHolder {public TextView titleView;} }
使用ViewHolder的好处就是对控件进行缓存, 以便下次取方便,提高访问效率。
2. 接下来就是对类别中的美女图片的显示了,
定义xml布局文件如下:
<RelativeLayout 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" >
<ImageView android:id="@+id/large_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
<TextView android:id="@+id/tip"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="@string/slide_tip"
android:gravity="center_vertical"
android:textColor="#dd0000"
android:layout_above="@+id/gallery"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:gravity="center"
android:padding="5dp"
android:layout_alignParentBottom="true"
android:paddingBottom="10dp" />
<LinearLayout
android:id="@+id/miniAdLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
/>
</RelativeLayout>
类代码如下:
public class ViewActivity extends Activity implements OnItemSelectedListener {private Gallery mGallery;private ImageAdapter mAdapter;LayoutInflater mInflater;private ImageView largeView;private Animation mAnimation;private int categoryId;private String imgDir = "";private Bitmap[] imageUri;private int showCount = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.setTitle(getIntent().getIntExtra(Constants.KEY_TITLE, R.string.app_name)); mGallery = (Gallery) findViewById(R.id.gallery); mInflater = LayoutInflater.from(this); mAdapter = new ImageAdapter(this); largeView = (ImageView) findViewById(R.id.large_image); largeView.setScaleType(ImageView.ScaleType.FIT_XY); mAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha_ani); largeView.startAnimation(mAnimation); } public void onResume() { super.onResume(); showCount++; if (getIntent().getBooleanExtra(Constants.KEY_AD, false)) { if (showCount == 1) { AppConnect.getInstance(this).showOffers(this); } } else { //build from asset initFromIntent(); mGallery.setAdapter(mAdapter); mGallery.setOnItemSelectedListener(this); } } private void initFromIntent() { //set title Intent intent = getIntent(); categoryId = getIntent().getIntExtra(Constants.KEY_CATEGORY, 0); String basePath = "images/"; switch (categoryId) { //suggest hot images case 0: imgDir = "jingxuantuijian"; break; //super starts case 1: imgDir = "mingxing"; break; //sexy girls case 2: imgDir = "xinggan"; break; //cosplay case 3: imgDir = "cosplay"; break; case 4: imgDir = "socks"; break; default: imgDir = "xinggan"; break; } try { basePath += imgDir; AssetManager am = this.getAssets();String[] files = am.list(basePath);imageUri = new Bitmap;basePath += "/";for(int i = 0; i < files.length; i++) {imageUri = BitmapFactory.decodeStream(am.open(basePath + files));}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} } private class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context context) { mContext = context; }@Overridepublic int getCount() {// TODO Auto-generated method stubreturn imageUri.length;}@Overridepublic Object getItem(int arg0) {// TODO Auto-generated method stubreturn imageUri;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder vh = null;if (convertView == null) {convertView = mInflater.inflate(R.layout.item, null);vh = new ViewHolder();vh.imgV = (ImageView) convertView.findViewById(R.id.imageV);vh.imgV.setScaleType(ImageView.ScaleType.FIT_XY);convertView.setTag(vh);} else {vh = (ViewHolder) convertView.getTag();}//set image resourcevh.imgV.setImageBitmap(imageUri);return convertView;}/** * holder for caching UI component * @author hp * */class ViewHolder {public ImageView imgV;} }@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1, int position,long arg3) {//recyle old bitmapBitmapDrawable drawable = (BitmapDrawable) largeView.getDrawable();if (drawable != null && !drawable.getBitmap().isRecycled()) {drawable.getBitmap().recycle();}largeView.setImageBitmap(imageUri);largeView.startAnimation(mAnimation);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {}}
中间大图片的显示加上动画,
动画文件如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="1500"/>
</set>
放在res/anim下。
实际效果请运行附件。
至此可完成。
页:
[1]