Android开发之简单的电子相册实现

电子相册的效果图和结构图: 

    


图片资源的文件:

       

  package com.example.electronicalbum;


public interface ImageResource {
  //用一个Integer数组保存图像资源
 Integer[] dImageID={
 R.drawable.a, 
 R.drawable.b,
 R.drawable.c,
 R.drawable.d,
 R.drawable.e, 
 R.drawable.f,
 
 };
}

布局的文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bj"
    >
  
  
<Gallery android:id="@+id/gallery"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content">  
</Gallery>  
   
<ImageView android:id="@+id/imageView"  
    android:layout_height="fill_parent"  
    android:layout_width="fill_parent">  
</ImageView>

</LinearLayout>

  提到的string.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">电子相册</string>
    <string name="action_settings">Settings</string>
 
 <declare-styleable name="Gallery1">  
        <attr name="android:galleryItemBackground" />  
    </declare-styleable>  
     <declare-styleable name="LabelView">  
        <attr name="text" format="string" />  
        <attr name="textColor" format="color" />  
        <attr name="textSize" format="dimension" />  
    </declare-styleable> 
</resources>


最主要的代码和注释:希望对大家有帮助:



package com.example.electronicalbum;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;


public class MainActivity extends Activity {
      //用来显示单个图像
  private ImageView imageOne;
  
 //用来显示所有的图像
  private ImageView imageAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageOne = (ImageView) findViewById(R.id.imageView);
//新建一个Gallery类,这是是实现拖动效果的关键   
       //Gallery is 一个用来水平卷动显示对象的视图  
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
}
//实现ImageResource接口获得自己创建的图像资源
class ImageAdapter extends BaseAdapter implements ImageResource{
//每一个gallery中图像的背景资源   
   private int galleryItemBackground; 
   private Context context;


public ImageAdapter(Context context) {  
       this.context = context;
       //这里实现的功能上就是上半部分每个图像那个背景框
       // 对应的xml文件就是attr.xml
       TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
       galleryItemBackground = a.getResourceId( R.styleable.Gallery1_android_galleryItemBackground, 0);  
            a.recycle();  


}
@Override
public int getCount() {

return dImageID.length;
}


@Override
public Object getItem(int position) {

return position;
}


@Override
public long getItemId(int position) {
//将此索引的图像设为imageOne显示   
imageOne.setImageResource(ImageAdapter.dImageID[position]);   
             imageOne.setScaleType(ImageView.ScaleType.FIT_CENTER);
return position; 
}
 //这个方法返回的ImageView就是实现拖动效果的图像 
@Override
public View getView(int position, View convertView, ViewGroup parent) {
imageAll = new ImageView(context);
//设置图像源
imageAll.setImageResource(dImageID[position]);
//设置视图为120*120
imageAll.setLayoutParams(new Gallery.LayoutParams(125,125));
//设置图像相对于视图的比例,FIT_XY表示充满X和Y轴   
          imageAll.setScaleType(ImageView.ScaleType.FIT_XY);  
        //设置imageAll中每一个Item的背景资源 
          imageAll.setBackgroundResource(galleryItemBackground);
return imageAll;
}
 
}


@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;
}


}

请大家伙多多指教

邮箱:weimingweicom@sina.com

请关注:ailiandeziwei


    

  

原文地址:https://www.cnblogs.com/jiangu66/p/3155594.html