Android的ImageSwitcher效果
来源:互联网 发布:重新安装mac os x 编辑:程序博客网 时间:2024/05/16 19:07
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cn.npass.nj" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".ImageSwitcherDemoActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
imageswitcherpage.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:id="@+id/switcher_layout" android:layout_width="fill_parent" android:layout_height="450dp"> <ImageSwitcher android:id="@+id/switcher" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"/> <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"/> <ImageButton android:id="@+id/imgBtn_arrorleft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:src="@drawable/arror_left" /> <ImageButton android:id="@+id/imgBtn_arrorRight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/arror_right" /> </RelativeLayout> <RelativeLayout android:id="@+id/gallery_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/switcher_layout"> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:gravity="center_vertical" android:spacing="16dp" /> </RelativeLayout></RelativeLayout>
ImageSwitcherDemoActivity.java
package com.cn.npass.nj;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.view.Window;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.AnimationUtils;import android.view.animation.ScaleAnimation;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.Gallery;import android.widget.Gallery.LayoutParams;import android.widget.ImageButton;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;public class ImageSwitcherDemoActivity extends Activity {private ImageSwitcher mImageSwitcher;private Gallery mGallery;private ImageButton arrorLeft, arrorRight;private Button btnimageswitcherIn, btnimageswitcherOut;private AnimationSet manimationSet;private int idx = 0;private double scale;// 图片放大减小的比例private float scaleWidth = 1;private float scaleHeight = 1;private int[] mImageIds = { R.drawable.a1, R.drawable.a2, R.drawable.a3,R.drawable.a4, R.drawable.a5};private int[] mThumbIds = { R.drawable.a1, R.drawable.a2, R.drawable.a3,R.drawable.a4, R.drawable.a5 };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.imageswitcherpage);mImageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);// imageSwticher必须设置一个viewfactory后才可以查看图片mImageSwitcher.setFactory(new ImageViewFactory(this));mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));// 设置动画效果mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));// 设置动画效果mImageSwitcher.setOnClickListener(mOnClickListener);mGallery = (Gallery) findViewById(R.id.gallery);mGallery.setAdapter(new ImageAdapter(this, mThumbIds));mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id) {mImageSwitcher.setImageResource(mImageIds[position]);}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {}});arrorLeft = (ImageButton) findViewById(R.id.imgBtn_arrorleft);arrorLeft.setOnClickListener(mOnClickListener);arrorRight = (ImageButton) findViewById(R.id.imgBtn_arrorRight);arrorRight.setOnClickListener(mOnClickListener);}public OnClickListener mOnClickListener = new OnClickListener() {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.imgBtn_arrorleft:idx--;if (idx < 0) {idx = mImageIds.length - 1;}mGallery.setSelection(idx);break;case R.id.imgBtn_arrorRight:idx++;if (idx > mImageIds.length - 1) {idx = 0;}mGallery.setSelection(idx);break;case R.id.switcher://setImageScaleAnimation(v.getId(), mImageSwitcher);// 动画效果break;default:break;}}};/** * 图片减小放大 */private void ImageZoomInOut(double scale, ImageSwitcher imageSwitcher,int resId) {// 加载需要操作的图片Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), resId);// 获取这个图片的宽和高int width = bitmapOrg.getWidth();int height = bitmapOrg.getHeight();// 设置图片放大的比例this.scale = scale;// 计算这次要放大的比例scaleWidth = (float) (scaleWidth * scale);scaleHeight = (float) (scaleHeight * scale);// 创建操作图片用的matrix对象Matrix matrix = new Matrix();// 缩放图片动作matrix.postScale(scaleWidth, scaleHeight);// 旋转图片 动作// matrix.postRotate(45);// 创建新的图片Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,height, matrix, true);// 将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);// 设置ImageView的图片为上面转换的图片imageSwitcher.setImageDrawable(bmd);}/** * 设置动画放大的效果 * * @param position * @param view */private void setImageScaleAnimation(int position, ImageSwitcher view) {AnimationSet animationSet = new AnimationSet(true);ScaleAnimation scaleAnimation = new ScaleAnimation(2, 1.0f, 2, 1.0f,// 整个屏幕就0.0到1.0的大小//缩放 Animation.RELATIVE_TO_SELF, 0.5f, // 使用动画播放图片Animation.RELATIVE_TO_SELF, 0.5f);scaleAnimation.setDuration(1000);animationSet.addAnimation(scaleAnimation);animationSet.setFillAfter(true); // 让其保持动画结束时的状态。view.startAnimation(animationSet);}/** * */public class ImageViewFactory implements ViewFactory {private Context context;public ImageViewFactory(Context mcontext) {context = mcontext;}/** * 创建视图 */@Overridepublic View makeView() {// 定义每个图像的显示大小ImageView iv = new ImageView(context);iv.setBackgroundColor(0xFF000000);//设置精度类型iv.setScaleType(ImageView.ScaleType.FIT_CENTER);//设置布局参数iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));return iv;}}/** * */public class ImageAdapter extends BaseAdapter {private Context mContext;private int[] data;public ImageAdapter(Context c, int[] data) {this.mContext = c;this.data = data;}public int getCount() {return data.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 img = new ImageView(mContext);//设置图片资源img.setImageResource(data[position]);//设置与边界对齐img.setAdjustViewBounds(true);//设置布局参数img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));//设置背景资源img.setBackgroundResource(R.drawable.bg);return img;}}}
资源下载地址为:http://download.csdn.net/detail/niejing654092427/4501207
- Android的ImageSwitcher效果
- android ImageSwitcher的用法
- android ImageSwitcher的使用
- Android ImageSwitcher的用法!
- android ImageSwitcher的使用
- 具动画效果的图片转换器ImageSwitcher
- Android的ImageSwitcher实现原理
- Android开发之ImageSwitcher组件的使用以及设置图片切换的效果
- (转)Android 模仿淘宝滑动查看图片的效果 Gallery + ImageSwitcher
- Android ImageSwitcher
- Android ImageSwitcher
- android ImageSwitcher
- android ImageSwitcher
- android 用ImageSwitcher+Gallery实现图片浏览效果
- 【Android】图片切换组件ImageSwitcher的运用
- Android开发之ImageSwitcher组件的…
- Android 中ImageSwitcher的最简单用法
- Android中Gallery和ImageSwitcher的使用
- 谈 Spring-Transaction(Spring事务管理 第二篇)
- 谈 Spring-Transaction(Spring事务管理 第三篇)
- 常用Java开源库(转载)
- “降价风”来袭 RT版Surface仅售199刀?
- Java 遍历Map的几种方法
- Android的ImageSwitcher效果
- hdu 1754 I Hate It
- android 屏保相关文章
- execve函数簇
- autoit3——打开窗口
- 不要用retainCount来获得对象的引用次数
- 已知二叉树后序遍历序列是DBCEFGHA,中序遍历序列EDCBAHFG,它的前序遍历的序列是?
- Extjs-表格 Ext.grid.GridPanel
- Python对字母字符(串)中大小写转换函数--upper() 和 lower()