Android 自己製作的相簿–效果還不錯哦

來源:互聯網
上載者:User

 

這是我學習Android時做的一個小程式,程式主要功能是實現一個迷你相簿的功能,可以在虛擬機器上看到很不錯的效果。

我設定螢幕的大小為800*600

 

/*

*ImageSwitcherGallery.java

*/

 

package android.study_layout;

 

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;

 

public class ImageSwitcherGallery extends Activity implements
OnItemSelectedListener, ViewFactory {
private ImageSwitcher image_switcher;
private Gallery gallery;

private Integer[] mThumbIds = { R.drawable.a1, R.drawable.a2,
 R.drawable.a3, R.drawable.a4, R.drawable.a5,
 };

private Integer[] mImageIds = { R.drawable.a1, R.drawable.a2,
   R.drawable.a3, R.drawable.a4, R.drawable.a5, };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.image_switcher_gallery);

image_switcher = (ImageSwitcher) findViewById(R.id.switcher);
image_switcher.setFactory(this);

image_switcher.setInAnimation(AnimationUtils.loadAnimation(this,
  android.R.anim.fade_in));
image_switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
  android.R.anim.fade_out));

gallery = (Gallery) findViewById(R.id.gallery);

gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemSelectedListener(this);
}

@Override
public View makeView() {
ImageView image = new ImageView(this);
image.setBackgroundColor(0xFF000000);
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setLayoutParams(new ImageSwitcher.LayoutParams(
  LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return image;
}

public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
 mContext = c;
}

public int getCount() {
 return mThumbIds.length;
}

public Object getItem(int position) {
 return position;
}

public long getItemId(int position) {
 return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
 ImageView image = new ImageView(mContext);

 image.setImageResource(mThumbIds[position]);
 image.setAdjustViewBounds(true);
 image.setLayoutParams(new Gallery.LayoutParams(
   LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
 return image;
}

private Context mContext;

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
 long id) {
 ImageSwitcher image_switcher = (ImageSwitcher) findViewById(R.id.switcher);
 image_switcher.setImageResource(mImageIds[position]);

}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}

 

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="wrap_content"
    android:orientation="vertical">
   
<ImageSwitcher
 android:id="@+id/switcher"
 android:layout_width="match_parent"
 android:layout_height="450dip"
 android:layout_alignParentLeft="true"
 android:layout_alignParentTop="true"
/>

<Gallery
 android:id="@+id/gallery"
 android:layout_width="fill_parent"
 android:layout_height="120dip"
 android:background="#55000000"
 android:layout_alignParentLeft="true"
 android:layout_alignParentBottom="true"
 android:gravity="center_vertical"
 android:spacing="30dip"
/>

</RelativeLayout>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.