Gallery、ImageSwitcher使用实现画廊功能

 

1 ImageSwitcher是图片切换的控件,它能实现图片切换时的动画效果,包括图片导入效果、图片消失效果等等。

   Android系统提供了许多不同的动画效果供我们选择。

 

2  TypedArray obtainStyledAttributes 作用就是从我们自己定义的attr.xml读取所需信息 

3 Gallery 组件的作用是提供图片的缩图功能,它可以将好多的图片排列成一列让使用者浏览和点选,

它也会随着使用者的操作来自动转换图像的位置

 代码:

 1 Activity 

  

public class Gallery2Activity extends Activity implements ViewFactory,OnItemClickListener  {

 
    private ImageSwitcher imageSwitcher;  
    private Gallery gallery;  
   //保存图片的数组
   int[] imageIds = new int[] { R.drawable.beibei, R.drawable.huanhuan,
			R.drawable.jingjing, R.drawable.nini, R.drawable.yingying };
	
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        // 设置窗体无标题  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.activity_gallery2);  
        // 得到组件  
        imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1);  
        imageSwitcher.setFactory(this);  
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
                android.R.anim.fade_in));  
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,  
                android.R.anim.fade_out));  
  
        gallery = (Gallery) this.findViewById(R.id.gallery1);  
        gallery.setAdapter(new ImageAdapter());  
        //设置默认选择的图片  
        gallery.setSelection(imageIds.length/2);  
        //注册事件监听器  
        gallery.setOnItemClickListener(this);  
    }  
  
 
  
    @Override  
    public void onItemClick(AdapterView<?> adapterview, View view, int postion,  
            long id) {  
        imageSwitcher.setImageResource(imageIds[postion]);  
  
    }  
  
    // 重写视图工厂中的makeView方法,对ImageSwitcher显示的ImageView对象进行了设置  
    @Override  
    public View makeView() {  
        ImageView imageView = new ImageView(this);  
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(  
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
        return imageView;  
    }  
  
    /** 
     * 负责产生gallery中的图片 
     */  
    private class ImageAdapter extends BaseAdapter {  
        int mGalleryItemBackground;;  
        public ImageAdapter(){  
        	//获取自定义的背景属性值 
            TypedArray typedArray=obtainStyledAttributes(R.styleable.HelloGallery);  
            mGalleryItemBackground=typedArray.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);  
            typedArray.recycle();  //必须调用的方法
        }  
  
        // 返回图片的个数,比如你想得到图片的个数  
        @Override  
        public int getCount() {  
            return imageIds.length;  
        }  
  
        @Override  
        public Object getItem(int position) {  
            return imageIds[position];  
        }  
  
        @Override  
        public long getItemId(int position) {  
            return position;  
        }  
  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
  
            ImageView imageView = new ImageView(Gallery2Activity.this);  
            // 设置imageView中的图像资源  
            imageView.setImageResource(imageIds[position]);  
            /*// 设置图像大小尺寸自适应 
            imageView.setAdjustViewBounds(true);*/  
            imageView.setBackgroundResource(mGalleryItemBackground);  
            return imageView;  
        }  
    }  
}

 2   页面布局 

  

<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" >  
    <ImageSwitcher  
        android:id="@+id/imageSwitcher1"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true" >  
    </ImageSwitcher>  
  
    <Gallery  
        android:id="@+id/gallery1"  
        android:layout_width="fill_parent"  
        android:layout_height="60dp"  
        android:layout_alignParentBottom="true"  
        android:layout_alignParentLeft="true"  
        android:background="#55000000"  
        android:spacing="10dp" />  
</RelativeLayout> 

 3 自定义背景属性 values中的items.xml

   

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="HelloGallery">  
        <attr name="android:galleryItemBackground" />  
    </declare-styleable>  
</resources> 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Media3D; using System.Windows.Media.Effects; namespace WPFDemo { public partial class Rotate3DContainer : Grid, Container { private Storyboard front2BackStory; private Storyboard back2FrontStory; private Border frontWarpper; private Border backWarpper; public Rotate3DContainer() { InitializeComponent(); this.Loaded += new RoutedEventHandler(Container_Loaded); } private void Container_Loaded(object sender, RoutedEventArgs e) { Init(); } private void Init() { if (this.Children.Count == 2) { UIElement[] array = new UIElement[2]; this.Children.CopyTo(array, 0); this.Children.Clear(); frontWarpper = new Border() { Child = array[0], HorizontalAlignment=HorizontalAlignment.Left, VerticalAlignment=VerticalAlignment.Top, Background = Brushes.Transparent }; backWarpper = new Border() { Child = array[1], Opacity = 0, Visibility = Visibility.Hidden, HorizontalAlignment=HorizontalAlignment.Left, VerticalAlignment=VerticalAlignment.Top, Background = Brushes.Transparent }; Viewport3D viewport = Get3DView(); viewport.HorizontalAlignment = HorizontalAlignment.Left; viewport.VerticalAlignment = VerticalAlignment.Top; viewport.Height = frontWarpper.Child.DesiredSize.Height; viewport.Width = frontWarpper.Child.DesiredSize.Width; front2BackStory = new Storyboard() { Children = new TimelineCollection() { GetShowHideAnimation(viewport, 0, 1100), GetShowHideAnimation(backWarpper, 1000, -1), GetShowHideAnimation(frontWarpper, -1, 50), GetFadeAnimation(frontWarpper, 0, -1, 50), GetFadeAnimation(backWarpper, 1, 1050, 50), GetCameraMoveAnimation(0, 0, 0.5, 0, 0, 1.1, 50, 500,viewport), GetRotateAnimation(0, -180, 0.3, 0.3, 50, 1000) } }; back2FrontStory = new Storyboard() { Children = new TimelineCollection() { GetShowHideAnimation(viewport, 0, 1100), GetShowHideAnimation(frontWarpper, 1000, -1), GetShowHideAnimation(backWarpper, -1, 50), GetFadeAnimation(backWarpper, 0, -1, 50), GetFadeAnimation(frontWarpper, 1, 1050, 50), GetCameraMoveAnimation(0, 0, 0.5, 0, 0, 1.1, 50, 500, viewport), GetRotateAnimation(180, 360, 0.3, 0.3, 50, 1000) } }; this.Effect = new DropShadowEffect() { BlurRadius = 10, Opacity = 0.8 }; this.Children.Add(frontWarpper); this.Children.Add(backWarpper); this.Children.Add(viewport); array = null; } } private DoubleAnimation GetFadeAnimation(UIElement target, int toOpacity, int beginTime, int duration) { DoubleAnimation result = new DoubleAnimation(toOpacity, new Duration(TimeSpan.FromMilliseconds(duration))); if (beginTime >= 0) { result.BeginTime = TimeSpan.FromMilliseconds(beginTime); } Storyboard.SetTarget(result, target); Storyboard.SetTargetProperty(result, new PropertyPath("Opacity")); return result; } private Point3DAnimation GetCameraMoveAnimation(double x1, double y1, double z1, double x2, double y2, double z2, int beginTime, int duration, Viewport3D view) { Point3DAnimation result = new Point3DAnimation(new Point3D(x1, y1, z1), new Point3D(x2, y2, z2), new Duration(TimeSpan.FromMilliseconds(duration))) { AutoReverse = true, BeginTime = TimeSpan.FromMilliseconds(beginTime), DecelerationRatio = 0.3 }; Storyboard.SetTarget(result, view); Storyboard.SetTargetProperty(result, new PropertyPath("Camera.Position")); return result; } private DoubleAnimation GetRotateAnimation(double fromDegree, double toDegree, double accelerationRatio, double decelerationRatio, int beginTime, int duration) { DoubleAnimation result = new DoubleAnimation(fromDegree, toDegree, new Duration(TimeSpan.FromMilliseconds(duration)), FillBehavior.HoldEnd) { AccelerationRatio = accelerationRatio, DecelerationRatio = decelerationRatio, BeginTime = TimeSpan.FromMilliseconds(beginTime) }; Storyboard.SetTargetName(result, "AxisAngleRotation3D"); Storyboard.SetTargetProperty(result, new PropertyPath("Angle")); return result; } Viewport3D Get3DView() { Viewport3D viewport = new Viewport3D() { Camera = new PerspectiveCamera(new Point3D(0, 0, 0.5), new Vector3D(0, 0, -1), new Vector3D(0, 1, 0), 90), Visibility = Visibility.Hidden }; viewport.Children.Add(GetLightVisual3D()); viewport.Children.Add(GetSceneVisual3D(frontWarpper, backWarpper)); return viewport; } ModelVisual3D GetLightVisual3D() { Model3DGroup group = new Model3DGroup(); group.Children.Add(new DirectionalLight(Color.FromRgb(0x44, 0x44, 0x44), new Vector3D(0, 0, -1))); group.Children.Add(new AmbientLight(Color.FromRgb(0xBB, 0xBB, 0xBB))); return new ModelVisual3D() { Content = group }; } ModelVisual3D GetSceneVisual3D(Border frontElement, Border backElement) { MeshGeometry3D meshgmod = new MeshGeometry3D() { TriangleIndices = new Int32Collection(new int[] { 0, 1, 2, 2, 3, 0 }), TextureCoordinates = new PointCollection(new Point[]{new Point(0, 1),new Point(1, 1),new Point(1, 0),new Point(0, 0)}), Positions = new Point3DCollection(new Point3D[]{new Point3D(-0.5, -0.5, 0),new Point3D(0.5, -0.5, 0),new Point3D(0.5, 0.5, 0),new Point3D(-0.5, 0.5, 0)}) }; VisualBrush frontBrush = new VisualBrush(frontElement.Child) { Stretch = Stretch.Uniform }; VisualBrush backBrush = new VisualBrush(backElement.Child) { Stretch = Stretch.Uniform, RelativeTransform = new ScaleTransform(-1, 1, 0.5, 0) }; AxisAngleRotation3D rotate = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0); this.RegisterName("AxisAngleRotation3D", rotate); GeometryModel3D gmode3d = new GeometryModel3D() { Geometry = meshgmod, Material = new DiffuseMaterial(frontBrush), BackMaterial = new DiffuseMaterial(backBrush), Transform = new RotateTransform3D(rotate) }; return new ModelVisual3D() { Content = gmode3d }; } ObjectAnimationUsingKeyFrames GetShowHideAnimation(UIElement element, int showTime, int hideTime) { ObjectAnimationUsingKeyFrames frame = new ObjectAnimationUsingKeyFrames(); Storyboard.SetTarget(frame, element); Storyboard.SetTargetProperty(frame, new PropertyPath("Visibility")); if (showTime >= 0) { frame.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Visible, TimeSpan.FromMilliseconds(showTime))); } if (hideTime >= 0) { frame.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Hidden, TimeSpan.FromMilliseconds(hideTime))); } return frame; } public void Turn(bool isReverse) { Storyboard target = null; DoubleAnimation direction = null; double fromAngle = 0; double step = 180; if (frontWarpper.Opacity != 0) { target = front2BackStory; } else { fromAngle = 180; target = back2FrontStory; } direction = (DoubleAnimation)target.Children[6]; if (!isReverse) { step = -180; } direction.From = fromAngle; direction.To = fromAngle + step; target.Begin(this); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值