首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

Android38_ImageView跟Gallery

2012-06-26 
Android38_ImageView和Gallery一、ImageView使用方法?xml version1.0 encodingutf-8?LinearLayout

Android38_ImageView和Gallery

一、ImageView使用方法

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试"/><ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/image"/></LinearLayout>

?ImageViewActivity.java

package com.android.activity;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.RotateAnimation;import android.widget.Button;import android.widget.ImageView;public class ImageViewActivity extends Activity {private Button button = null;private ImageView image = null;@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        button = (Button)findViewById(R.id.button);        image = (ImageView)findViewById(R.id.image);        button.setOnClickListener(new ButtonListener());    }class ButtonListener implements OnClickListener{public void onClick(View v) {AnimationSet animationSet = new AnimationSet(true);//淡入淡出AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);alphaAnimation.setDuration(1000);//旋转RotateAnimation rotateAnimation = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);rotateAnimation.setDuration(1000);animationSet.addAnimation(alphaAnimation);animationSet.addAnimation(rotateAnimation);image.startAnimation(animationSet);}}}

?运行结果:既旋转有淡出

Android38_ImageView跟Gallery

?

<Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:animationDuration="100" android:unselectedAlpha="0.5" android:spacing="2dp" android:gravity="center_vertical"> </Gallery>?

package com.android.activity;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter{// 定义Contextprivate Context context;// 定义整型数组 即图片源private Integer[] images = {R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11, R.drawable.img12, };// 声明 ImageAdapterpublic ImageAdapter(Context c){context = c;}// 获取图片的个数public int getCount(){return images.length;}// 获取图片在库中的位置public Object getItem(int position){return position;}// 获取图片IDpublic long getItemId(int position){return position;}/** * 可以设置多种效果 */public View getView(int position, View convertView, ViewGroup parent){ImageView imageview = new ImageView(context);// 给ImageView设置资源imageview.setImageResource(images[position]);// 设置布局 图片120×120显示imageview.setLayoutParams(new Gallery.LayoutParams(192, 120));// 设置显示比例类型imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);return imageview;}}?

??????? 3.得到Gallery对象,设置Adapter,以及监听器。

?package com.android.activity;

import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Gallery;import android.widget.Toast;public class GalleryActivity extends Activity {Gallery gallery = null;@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //获得Gallery对象          gallery = (Gallery) findViewById(R.id.gallery);          //添加ImageAdapter给Gallery对象          gallery.setAdapter(new ImageAdapter(this));          //添加监听器        gallery.setOnItemClickListener(new ImageItemClickListener());    }class ImageItemClickListener implements OnItemClickListener{public void onItemClick(AdapterView<?> parent, View v, int position, long id) { System.out.println("选择了"+(position+1)+"张图片");  }}}

?运行结果:

Android38_ImageView跟Gallery

?

热点排行