如何在android上全屏显示图像

时间:2013-06-13 23:23:57

标签: android fullscreen sd-card

我想制作图库应用程序,从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;
    }      
}

我刚开始编程,所以请给出一些解释,如果你也提供完整的代码,那将会很棒:)

2 个答案:

答案 0 :(得分: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};

}

<强> main.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>

答案 1 :(得分:0)

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