• 是男人就下100层【第二层】——帮美女更衣(1)


    去年有一段时间有一个应用下载量很高,都是男同志的功劳,现在又出了第二版,第二层我们就来实现一下《帮美女更衣》这个应用,很多男生应该熟悉这个应用了吧,这里我就不解释里面的内容了,偷笑呵呵。

    先看看我们要实现的效果:

                 (惨不忍睹!做好后大家去挖剩下的吧)

    先看看布局文件activity_main.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" >
    
        <ImageSwitcher
            android:id="@+id/image_switcher"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />
    
        <Gallery
            android:id="@+id/gallery"
            android:layout_width="fill_parent"
            android:layout_height="80.0dip"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="#55000000"
            android:gravity="center_vertical"
            android:spacing="15.0dip" />
    
    </RelativeLayout>

    ImageSwitcher继承自ViewSwitcher,重写了showNext()、showPrevious()方法。

    使用ImageSwithcer的步骤如下:

    1、为ImageSwitcher提供一个ViewFactory,该ViewFactory会有一个makeView()方法来生成View组件,这个组件必须是ImageView.

    2、切换图片时只需要调用 setImageDrawable(Drawable drawable)方法、setImageResource(int resid)方法或setImageURI(Uri uri).

    package com.example.mygrilproject;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.Gallery.LayoutParams;
    import android.widget.ImageSwitcher;
    import android.widget.ImageView;
    import android.widget.ImageView.ScaleType;
    import android.widget.ViewSwitcher.ViewFactory;
    
    public class MainActivity extends Activity implements ViewFactory{
    	private ImageSwitcher is;
    	private Gallery gallery;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		//获取ImageSwitcher对象
    		is = (ImageSwitcher)findViewById(R.id.image_switcher);
    		
    		is.setFactory(this);
    		//获取gallery对象
    		gallery = (Gallery)findViewById(R.id.gallery);
    		//设置适配器
    		gallery.setAdapter(new ImageAdapter(MainActivity.this));
    		//设置监听
    		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
    			@Override
    			public void onItemSelected(AdapterView<?> parent, View view,
    					int position, long id) {
    				int drawableId = 0;
        			try {
        				drawableId = R.drawable.class.getDeclaredField(
        						"pre" + position).getInt(this);
        			} catch (IllegalArgumentException e) {
        				e.printStackTrace();
        			} catch (SecurityException e) {
        				e.printStackTrace();
        			} catch (IllegalAccessException e) {
        				e.printStackTrace();
        			} catch (NoSuchFieldException e) {
        				e.printStackTrace();
        			}
        			//更换ImageSwitcher中的图片
                	is.setImageResource(drawableId);
    				
    			}
    			@Override
    			public void onNothingSelected(AdapterView<?> parent) {
    			}  
            });
    		
    		is.setOnClickListener(new OnClickListener() {
    			@Override
    			public void onClick(View v){
    				
    			}
    		});
    	}
    
    	//设置图片切换的动画效果
    	@Override
    	public View makeView() {
    		ImageView i = new ImageView(this);
    		i.setBackgroundColor(0xFF000000);
    		i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    		i.setLayoutParams(new ImageSwitcher.LayoutParams(
    				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    		return i;
    	}
    	
    	private class ImageAdapter extends BaseAdapter {
    		private Context mContext;
    		public ImageAdapter(Context c){
    			mContext = c;
    		}
    		@Override
    		public int getCount() {
    			return 11;//11张图片
    		}
    		@Override
    		public Object getItem(int position) {
    			return position;
    		}
    		@Override
    		public long getItemId(int position) {
    			return position;
    		}
    
    		@Override
    		public View getView(int position, View convertView, ViewGroup parent) {
    			ImageView imageView = new ImageView(mContext);
    			int drawableId = 0;
    			try {
    				//使用反射获取资源对象
    				drawableId = R.drawable.class.getDeclaredField(
    						"pre" + position).getInt(this);
    			} catch (IllegalArgumentException e) {
    				e.printStackTrace();
    			} catch (SecurityException e) {
    				e.printStackTrace();
    			} catch (IllegalAccessException e) {
    				e.printStackTrace();
    			} catch (NoSuchFieldException e) {
    				e.printStackTrace();
    			}
    			imageView.setLayoutParams(new Gallery.LayoutParams(120,120));
    			imageView.setScaleType(ScaleType.FIT_XY);
    			imageView.setImageResource(drawableId);
    			return imageView;
    		}
    	}
    
    }
    
    运行效果:



    下一篇我们再来看看如何撕衣服,呵呵大笑

    源代码下载:http://download.csdn.net/detail/lxq_xsyu/7014543


  • 相关阅读:
    SSH出现Connection refused错误
    Lisp经典算法
    Arch Linux下韩文重叠显示
    Vim在图形环境下全屏产生黑边
    Vim常用插件安装及配置方法
    Python中Scrapy框架元素选择器XPath的简单实例
    Manjaro下Steam无法启动
    GNOME禁用GDM中night-light功能
    Neovim中提示Error: Required vim compiled with +python
    Manjaro下带供电的USB Hub提示error -71
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6469143.html
Copyright © 2020-2023  润新知