Android开发学习笔记(四):两种方法实现图片播放器

来源:互联网 发布:300出装软件 编辑:程序博客网 时间:2024/05/18 23:57

也许有朋友想问小木为什么要学习写图片播放器呢?对于一个初学者来说,Android的页面布局,以及一些简单控件的使用是必备的基础知识,也是今后要常用的。再者,小木的学习方法是先找一些小程序做,在一定的时间段完成,以确保自己的学习进度。个人感觉单纯看视频,书籍,不动手是不行的,所以将这几天学习的两种图片播放器分享给大家,希望可以帮助到大家。

一:是基于ImageSwitcher和Button的图片播放器。

activity_image_switcher.xml中代码如下:

<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="200px"        android:layout_height="200px"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"         android:animateFirstView="true"                android:inAnimation="@android:anim/fade_in"        android:outAnimation="@android:anim/fade_out">    </ImageSwitcher>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/imageSwitcher1"        android:layout_below="@+id/imageSwitcher1"        android:text="下一张" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/button1"        android:layout_alignBottom="@+id/button1"         android:layout_alignLeft="@+id/imageSwitcher1"        android:text="上一张" /></RelativeLayout>


ImageSwitcherActivity.java中代码如下:

package com.example.imageswitcher;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;import android.app.Activity;public class ImageSwitcherActivity extends Activity {private Button buttonPre,buttonNext;private ImageSwitcher imageSwitcher;private int[] imageIds = {R.drawable.baiyang, R.drawable.chunv,R.drawable.jinniu,R.drawable.juxie, R.drawable.mojie,R.drawable.sheshou,R.drawable.shizi, R.drawable.shuangyu,R.drawable.shuangzi,R.drawable.shuiping, R.drawable.tiancheng,R.drawable.tianxie,};private int index=0;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_image_switcher);         imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);               imageSwitcher.setFactory(new ViewFactory() {public View makeView() {ImageView imageView = new ImageView(ImageSwitcherActivity.this);imageView.setBackgroundColor(0xff0000);imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);imageView.setLayoutParams(new ImageSwitcher.LayoutParams(200,200));return imageView;}});        imageSwitcher.setBackgroundResource(imageIds[index]);//        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));                       buttonNext = (Button)findViewById(R.id.button1);        buttonNext.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(index>=0&&index<imageIds.length-1)     {      index++;      imageSwitcher.setBackgroundResource(imageIds[index]);     }else     {      index=imageIds.length-1;     }}});                buttonPre = (Button)findViewById(R.id.button2);        buttonPre.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(index>0&&index<imageIds.length)     {      index--;     imageSwitcher.setBackgroundResource(imageIds[index]);     }else     {      index=imageIds.length-1;     }}});            }    }

运行截图如下:



二:基于Imageswitcher与Gallery的图片播放器:
activity_main.xml中的代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:background="@android:color/black"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.galleryplayer.MainActivity"    tools:ignore="MergeRootFrame" >    <ImageSwitcher        android:id="@+id/imageswitcher"        android:layout_width="320dp"        android:layout_height="270dp" />    <Gallery        android:id="@+id/gallery"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:background="@android:color/black"        android:gravity="center_vertical"         android:spacing="3pt"/></RelativeLayout>

MainActivity.java中代码如下:
package com.example.galleryplayer;import java.util.HashMap;import android.content.Context;import android.content.res.TypedArray;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;@SuppressWarnings("deprecation")public class MainActivity extends ActionBarActivity implements OnItemSelectedListener, ViewFactory, OnItemClickListener {@SuppressWarnings("deprecation")private Gallery gallery;          private ImageSwitcher imageSwitcher;          private ImageAdapter imageAdapter;          private int mCurrentPos = -1;// 当前的item          private HashMap<Integer, ImageView> mViewMap;                private int[] resIds = new int[]              {        R.drawable.item1 ,R.drawable.item2 ,R.drawable.item3 ,R.drawable.item4 ,        R.drawable.item5 ,R.drawable.item6 ,R.drawable.item7 ,R.drawable.item8 ,        };    @SuppressWarnings("deprecation")@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        gallery = (Gallery) findViewById(R.id.gallery);          imageAdapter = new ImageAdapter(this, resIds.length);          gallery.setAdapter(imageAdapter);          gallery.setOnItemSelectedListener(this);          gallery.setSelection(1);// 设置一加载Activity就显示的图片为第二张            gallery.setOnItemClickListener(this);            imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);          imageSwitcher.setFactory(this);            // 设置动画效果 淡入淡出          imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));          imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));      }            public class ImageAdapter extends BaseAdapter      {          int mGalleryItemBackground;          private Context mContext;          private int mCount;// 一共多少个item            public ImageAdapter(Context context, int count)          {              mContext = context;              mCount = count;              mViewMap = new HashMap<Integer, ImageView>(count);             // TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);              // 设置边框的样式            //  mGalleryItemBackground = typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);          }            public int getCount()          {              return Integer.MAX_VALUE;          }            public Object getItem(int position)          {              return position;          }            public long getItemId(int position)          {              return position;          }            public View getView(int position, View convertView, ViewGroup parent)          {              ImageView imageview = mViewMap.get(position % mCount);              if (imageview == null)              {                  imageview = new ImageView(mContext);                  imageview.setImageResource(resIds[position % resIds.length]);                  imageview.setScaleType(ImageView.ScaleType.FIT_XY);                  imageview.setLayoutParams(new Gallery.LayoutParams(136, 88));                  imageview.setBackgroundResource(mGalleryItemBackground);              }              return imageview;          }      }      // 滑动Gallery的时候,ImageView不断显示当前的item      @Override      public void onItemSelected(AdapterView<?> parent, View view, int position, long id)      {          // imageSwitcher.setImageResource(resIds[position % resIds.length]);      }        // 设置点击Gallery的时候才切换到该图片      @Override      public void onItemClick(AdapterView<?> parent, View view, int position, long id)      {          if (mCurrentPos == position)          {              // 如果在显示当前图片,再点击,就不再加载。              return;          }          mCurrentPos = position;          imageSwitcher.setImageResource(resIds[position % resIds.length]);      }        @Override      public void onNothingSelected(AdapterView<?> parent)      {      }        @Override      public View makeView()      {          ImageView imageView = new ImageView(this);          imageView.setBackgroundColor(0xFF000000);          imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);          imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));            return imageView;      }            @Override    public boolean onCreateOptionsMenu(Menu menu) {                // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    /**     * A placeholder fragment containing a simple view.     */    }

运行结果如下:

源代码下载地址:
http://download.csdn.net/detail/u013671350/7635049
http://download.csdn.net/detail/u013671350/7635041


0 0
原创粉丝点击