一、ImageView使用方法
ImageView可以用来显示任意的图像,例如一个图标等。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。
常用的属性:
android:adjustViewBounds - 是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。
android:maxHeight - 设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。
android:maxWidth - 设置View的最大宽度。
android:scaleType - 设置图片的填充方式。
android:src - 设置View的drawable(如图片,也可以是颜色,但是需要指定View的大小)
android:cropToPadding - 是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用。
android:tint - 将图片渲染成指定的颜色。
例子:
Main.xml
<?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); } } }
运行结果:既旋转有淡出
二、Gallery使用方法
Gallery是Android中的图片画廊控件。Gallery 是一个非常炫的效果,可以直接拖动图片来进行移动。使用很简单,只需要使用一个容器来存放Gallery显示的图片,这是使用一个继承自BaseAdapter类的派生类来装这些图片即可。然后可以监听事件setOnItemClickListener,从而确定用户选中的是哪一张图片即可。
1.设置图片画廊布局
android:animationDuration - 设置布局变化时动画的转换所需的时间
android:unselectedAlpha - 设置未选中的条目的透明度(Alpha),该值必须是float类型
android:spacing - 设置图片之间的间距
android:gravity - 指定在对象的X和Y轴上如何放置内容
main.xml
<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>
2.向Gallery控件中添加内容
如果向控件Gallery里面添加内容,必须继承一个类:BaseAdapter,然后重写他的部分函数。
ImageAdapter.java
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{ // 定义Context private Context context; // 定义整型数组 即图片源 private Integer[] images = { R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11, R.drawable.img12, }; // 声明 ImageAdapter public ImageAdapter(Context c){ context = c; } // 获取图片的个数 public int getCount(){ return images.length; } // 获取图片在库中的位置 public Object getItem(int position){ return position; } // 获取图片ID public 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)+"张图片"); } } }
运行结果: