也許有朋友想問小木為什麼要學習寫圖檔播放器呢?對于一個初學者來說,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 stub
if(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 stub
if(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