1

我想制作从 SD 卡中的文件夹加载图像的图库应用程序,当我单击其中一张图像时,它将全屏显示图像。现在,我可以在网格视图中显示所有图像,但我不知道如何将其显示为全屏。

任何机构可以帮助我?

这是我的完整代码:

import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private ImageAdapter imageAdapter;
    ArrayList<String> f = new ArrayList<String>();// list of file paths
    File[] listFile;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getFromSdcard();
        GridView imagegrid = (GridView) findViewById(R.id.ImageGrid);
        imageAdapter = new ImageAdapter();
        imagegrid.setAdapter(imageAdapter);       
}

    public void getFromSdcard()
    {
        File file= new File(android.os.Environment.getExternalStorageDirectory(),"images");

        if (file.isDirectory())
        {
            listFile = file.listFiles();


            for (int i = 0; i < listFile.length; i++)
            {

                f.add(listFile[i].getAbsolutePath());

            }
        }
    }


    public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount() {
            return f.size();
        }

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(
                        R.layout.gallery, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }

            Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
            holder.imageview.setImageBitmap(myBitmap);
            return convertView;
        }        
    }
    class ViewHolder {
        ImageView imageview;
    }      
}

我只是从编程开始,所以请给出一些解释,或者如果你也给出完整的代码那就太好了:)

4

2 回答 2

1

首先在放置图像的资源中创建一个可绘制文件夹

MainActivity.java

 public class GalleryScreen extends Activity implements
  AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
AlertDialog.Builder myAlertDialog;
Context context;
int newpos=0;
 private ImageView mSwitcher;

 WallpaperManager myWallpaperManager; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery);
  }

public void onResume()
{
super.onResume();

context=this;

myWallpaperManager = WallpaperManager.getInstance(context);

 mSwitcher = (ImageView) findViewById(R.id.imageviewid);


 mSwitcher.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            myAlertDialog = new AlertDialog.Builder(context);
            // myAlertDialog.setTitle("--- Title ---");
             myAlertDialog.setMessage("Do you want to set as wallpaper");
             myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

              public void onClick(DialogInterface arg0, int arg1) {


                    try {
                        myWallpaperManager.setResource(mThumbIds[newpos]);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
              // do something when the OK button is clicked
              }});
             myAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

              public void onClick(DialogInterface arg0, int arg1) {
                  arg0.cancel();
              // do something when the Cancel button is clicked
              }});
             myAlertDialog.show();



        }
    });

 Gallery g = (Gallery) findViewById(R.id.galleryid);
 g.setAdapter(new ImageAdapter(context));
 g.setOnItemSelectedListener(this);



}

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    mSwitcher.setImageResource(mThumbIds[position]);
    newpos=position;
}

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

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



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 i = new ImageView(mContext);

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

        return i;
    }

    private Context mContext;

}

private static final Integer[] mThumbIds = {
        R.drawable.nature, R.drawable.lilyflowerplant,
        R.drawable.heartlovecomputer, R.drawable.dolphin,R.drawable.youtube,R.drawable.twitter,R.drawable.sunset,R.drawable.androidplatform};

private Integer[] mImageIds = {
        R.drawable.nature};

}

主要的.xml

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
                                                                        android:layout_width="match_parent" 
    android:layout_height="match_parent"  
    android:background="@drawable/bg">

<RelativeLayout 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:background="@drawable/header">

 <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/logo" 
     android:layout_centerHorizontal="true"/>
 </RelativeLayout>

 <ImageView 
     android:id="@+id/imageviewid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true"/>

 <Gallery 
     android:id="@+id/galleryid" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" .
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:gravity="center_vertical" 
     android:spacing="16dp"/>
  </RelativeLayout>
于 2013-06-14T00:26:14.263 回答
0

好吧,首先我会在网格视图上设置一个 onclickListener(),它会调用一个方法来打开一个带有所需图像的活动。我不在我的电脑前,所以我不能真正输入任何代码,但我提到的应该会让你朝着正确的方向前进。祝你好运!

于 2013-06-13T23:50:50.420 回答