`
Bauble
  • 浏览: 66942 次
  • 性别: Icon_minigender_1
  • 来自: Mercury
社区版块
存档分类
最新评论

Android38_ImageView和Gallery

阅读更多

一、ImageView使用方法

       ImageView可以用来显示任意的图像,例如一个图标等。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。

       常用的属性:

android:adjustViewBounds - 是否保持宽高比。需要与maxWidthMaxHeight一起使用,否则单独使用没有效果。

android:maxHeight - 设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。

android:maxWidth - 设置View的最大宽度。

android:scaleType - 设置图片的填充方式。

android:src - 设置Viewdrawable(如图片,也可以是颜色,但是需要指定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使用方法

GalleryAndroid中的图片画廊控件。Gallery 是一个非常炫的效果,可以直接拖动图片来进行移动。使用很简单,只需要使用一个容器来存放Gallery显示的图片,这是使用一个继承自BaseAdapter类的派生类来装这些图片即可。然后可以监听事件setOnItemClickListener,从而确定用户选中的是哪一张图片即可。

       1.设置图片画廊布局

       android:animationDuration - 设置布局变化时动画的转换所需的时间

       android:unselectedAlpha - 设置未选中的条目的透明度(Alpha),该值必须是float类型

       android:spacing - 设置图片之间的间距

android:gravity - 指定在对象的XY轴上如何放置内容

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)+"张图片");  		}
	}
}

 运行结果:

 

  • 大小: 31.9 KB
  • 大小: 87.1 KB
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Android Gallery自定义ImageView和Gallery实现图片浏览功能

    Android Gallery 自定义ImageView和Gallery实现图片浏览功能,类似Android图库软件,主要实现的功能有:图片左右滑动、放大缩小、放大弹回、缩小弹回。 一开始使用Viewpager实现图片浏览,发现两张图片滑动时没有...

    android_3D_gallery,android

    再者,对于图片的加载和显示,Android提供了多种选择,如Bitmap类和ImageView组件。开发者可能会使用异步加载技术,比如Picasso或 Glide库,来优化图片加载速度,防止UI线程阻塞,提高用户体验。同时,为了在3D环境...

    Android_UI_Gallery

    总结起来,"Android_UI_Gallery"项目展示了如何利用`Gallery`组件创建一个可滚动的UI元素,并通过定制适配器、事件监听和动画来实现丰富的交互体验。虽然`Gallery`已被弃用,但其概念和技巧在理解和使用现代组件如`...

    Android应用源码之Gallery_Gallery.zip

    Gallery是Android平台上一个经典的图片浏览组件,它在早期版本的Android系统中被广泛使用,用于展示和选择图片。这个源码分析将深入探讨Gallery组件的工作原理、关键类和方法,以及如何自定义和优化它。 首先,让...

    Android应用源码之Gallery1_Gallery.zip

    10. **版本兼容性**:注意,Gallery控件在Android API 16(Jelly Bean)之后已被弃用,开发者应考虑使用RecyclerView的HorizontalScrollView替代,以获得更好的性能和更多的定制选项。 这个"Gallery1"项目可能包含...

    Multitouch 多点触摸ImageView效果

    为了实现多点触摸和Gallery的联动,我们需要在Gallery的OnItemSelectedListener接口中设置监听器,当用户选择新的图片时,更新ImageView的图片源并重置其缩放比例。同时,为了防止在缩放过程中Gallery的滑动冲突,...

    Gallery and ImageView 实例代码

    在Android开发中,`Gallery`和`ImageView`是两个非常重要的组件,主要用于展示图像和进行图片浏览。`Gallery`是一个可以水平滚动的视图,通常用于实现图片轮播或者选择器,而`ImageView`则是用来显示单个图像的组件...

    android 带有 动画 效果 的 Gallery 控件

    然而,原生的`Gallery`控件在Android API 17之后就被废弃了,但通过一些技巧和自定义实现,我们仍然可以在较新的API版本上实现类似的功能。本文将深入探讨如何创建一个带有动画效果的`Gallery`控件,并提供相关的...

    android_GalleryDEMO

    在`android_GalleryDEMO`这个示例中,我们将深入探讨`Gallery`组件的使用方法和相关知识点。 1. **`Gallery`的基本用法** - 在XML布局文件中,我们可以添加`&lt;Gallery&gt;`标签来创建一个`Gallery`控件。例如: ```...

    Android应用源码之Gallery2_Android.zip

    再者,Gallery2可能使用了ImageView和Bitmap的高效加载策略。在Android中,大图的直接加载可能导致内存溢出,因此通常会采用像LruCache这样的内存缓存或Picasso、 Glide等第三方库进行图片加载优化。通过源码,你...

    031_android UI组件之 Gallery画廊控件

    2. 创建一个适配器,例如使用ImageView和ArrayAdapter: ```java ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter(this, android.R.layout.simple_gallery_item, imageList); gallery.setAdapter(adapter); ``` ...

    android 循环gallery 选中放大 按键控制

    `Gallery`的XML属性包括`android:id`用于标识控件,`android:layout_width`和`android:layout_height`定义其尺寸,`android:spacing`设置项目之间的间隔,以及`android:fadingEdgeLength`定义边缘淡出长度,提高视觉...

    Android Gallery小例子

    这个小例子展示了如何在Android应用中集成和使用`Gallery`控件,以实现一个可滚动的图片画廊功能。下面我们将深入探讨`Gallery`的相关知识点。 1. **Gallery控件介绍** `Gallery`是Android SDK中的一个...

    Android下Gallery控件的使用

    1. 添加依赖:在XML布局文件中添加`Gallery`控件,通常设置`android:id`、`android:layout_width`和`android:layout_height`属性。 ```xml &lt;Gallery android:id="@+id/gallery" android:layout_width="match_...

    android gallery 放大效果

    在Android开发中,`Gallery`组件是一个非常有用的控件,它允许用户在水平轴上滑动浏览多个项目,常用于实现图片浏览或者选项选择。在本主题中,我们将深入探讨如何利用`Gallery`来实现图片滑动,并且在选中图片时...

    android开发,gallery图片切换

    在Android开发中,`Gallery`组件是一个非常实用的控件,它允许用户通过左右滑动来浏览和选择项目,常用于图片浏览、菜单选择等场景。在这个特定的案例中,我们关注的是如何实现图片的自动切换功能,这在创建图片轮播...

    Android 之 Gallery画廊用法

    尽管如此,理解`Gallery`的工作原理和用法仍然对学习Android的历史和迁移旧代码至新组件有所帮助。 `Gallery`的基本用法: 1. 在布局文件中添加`Gallery`组件: ```xml &lt;Gallery android:id="@+id/gallery" ...

    一个简单的android_gallery

    在Android开发中,`android_gallery`通常指的是一个用于展示图像的组件,类似于一个可以滚动的图片画廊。这个项目的核心功能是实现用户点击gallery组件中的图片后,将与之关联的另一组图片显示在一个`ImageSwitcher`...

    Android中gallery图片自动切换Demo

    在Android开发中,`Gallery`组件是用于展示一系列图片或者选项的一个控件,它允许用户通过左右滑动来浏览。然而,`Gallery`在新版本的Android SDK中已经被弃用,取而代之的是`HorizontalScrollView`或者`ViewPager`...

Global site tag (gtag.js) - Google Analytics