android图像“查看器”应用程序

时间:2010-10-03 17:31:40

标签: android image gallery

我正在尝试创建一个Android应用程序,让我可以全屏显示图像,顶部有下一个和上一个按钮,可以在它们之间进行切换。

有人能指点我一些教程,在那里我可以找到类似的东西的说明吗?

如果没有,用于将图像导入应用程序的最佳方法是什么?我已经尝试了几种方法来为图像创建对象类,并使用Bitmap Factory在每个图像中使用drawable实例化它以返回图像,但这不起作用。

我是android的初学者,可以真正使用参考资料,但找不到任何有用的内容。

2 个答案:

答案 0 :(得分:11)

作为一个新手,我一直在使用这个,这很简单。这里有一些代码(也许有更好的方法,但这是我弄清楚如何做的方式):

package com.imageviewexample;

import android.app.Activity;    
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class ImageViewExample extends Activity implements OnClickListener {

    /** Called when the activity is first created. */

    int image_index = 0;
    private static final int MAX_IMAGE_COUNT = 3;

    private int[] mImageIds = {
            R.raw.image1,
            R.raw.image2,
            R.raw.image3
    };

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

        Button btnPrevious = (Button)findViewById(R.id.previous_btn);
        btnPrevious.setOnClickListener(this);       
        Button btnNext = (Button)findViewById(R.id.next_btn);
        btnNext.setOnClickListener(this);

        showImage();        

    }

    private void showImage() {

        ImageView imgView = (ImageView) findViewById(R.id.myimage);             
        imgView.setImageResource(mImageIds[image_index]);       

    }

    public void onClick(View v) {

        switch (v.getId()) {

            case (R.id.previous_btn):

                image_index--;

                if (image_index == -1) {                    
                    image_index = MAX_IMAGE_COUNT - 1;                  
                }

                showImage();

            break;

            case (R.id.next_btn):

                image_index++;

                if (image_index == MAX_IMAGE_COUNT) {               
                image_index = 0;                
            }

                showImage();

            break;      

        }

    }
}

这是main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button
    android:id="@+id/previous_btn"
    android:layout_width="124dip" 
    android:layout_height="wrap_content" 
    android:text="Previous"
    />

<Button
    android:id="@+id/next_btn"
    android:layout_width="124dip" 
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/previous_btn" 
    android:text="Next"
    />

<ImageView
    android:id="@+id/myimage"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_below="@+id/previous_btn"
    />         

</RelativeLayout>

答案 1 :(得分:8)

这是一个图片查看器解决方案,具有图片资源的网格视图,您可以从中选择图片并将其显示为单个图片。在这个单一图片视图中,使用PictureViewer.java可以浏览投掷资源图片左右按钮。您还可以返回使用HelloGridViewActivity.java完成的网格视图。 ImageAdapter.java用于定义网格视图资源和方法。希望这会有所帮助:

HelloGridViewActivity.java:

package com.example.hellogridview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class HelloGridViewActivity extends Activity {

private long prev=0,next= 0;

@Override
public void onCreate(Bundle savedInstanceState) {



    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {


            prev = ImageAdapter.getPrevItemId(position);
            next = ImageAdapter.getNextItemId(position);

            showImage(gridview.getAdapter().getItemId(position),position);


        }
    });

}
private void showImage(long id, int pos){
    Intent pictureViewer = new Intent(this, PictureViewer.class);
    pictureViewer.putExtra("pictureId",id );
    pictureViewer.putExtra("picturePosition", pos);
    pictureViewer.putExtra("picturePrevId", prev);
    pictureViewer.putExtra("pictureNextId", next);

    startActivityForResult(pictureViewer,0);

  }
}    

ImageAdapter.java:

package com.example.hellogridview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

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

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

public long getItemId(int position) {
    return mThumbIds[position];
}

public static long getPrevItemId(int position) {
    if(--position<0) position = 21;
    return mThumbIds[position];
}

public static long getNextItemId(int position) {
    if(++position>21) position = 0;
    return mThumbIds[position];
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private static Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
  };
}    

PictureViewer.java:

package com.example.hellogridview;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class PictureViewer extends Activity {

long picItem,picPrevItem,picNextItem;
int picPosition;
private ImageView m_vwImage;


public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picture_layout);


    //citanje podataka prosledjenih ovoj aktivnosti
    //namera koja je pokrenula ovu aktivnost
    Intent i = getIntent();

    picItem = i.getLongExtra("pictureId", -1);
    picPrevItem = i.getLongExtra("picturePrevId", -1);
    picNextItem = i.getLongExtra("pictureNextId", -1);
    picPosition = i.getIntExtra("picturePosition", -1);
    m_vwImage = (ImageView) findViewById(R.id.imageview);


    //menjamo ulazne vrednosti da bi ih takve prosledili kao povratne
    Log.i("Nemanja", "picItem" + picItem);

    Drawable image = getResources().getDrawable((int)picItem);
    m_vwImage.setImageDrawable(image);


    Button startButton = (Button) findViewById(R.id.return_button);
    startButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            finish();   
        }
    });     

    Button leftButton = (Button) findViewById(R.id.left_button);
    leftButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            Drawable imagePrev = getResources().getDrawable((int)picPrevItem);
            m_vwImage.setImageDrawable(imagePrev);

            if(--picPosition<0) picPosition = 21;
            picPrevItem = (ImageAdapter.getPrevItemId(picPosition));
            picNextItem = (ImageAdapter.getNextItemId(picPosition));

        }
    }); 

    Button rightButton = (Button) findViewById(R.id.right_button);
    rightButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View view){
            Drawable imageNext = getResources().getDrawable((int)picNextItem);      
            m_vwImage.setImageDrawable(imageNext);

            if(++picPosition>21) picPosition = 0;
            picNextItem = (ImageAdapter.getNextItemId(picPosition));
            picPrevItem = (ImageAdapter.getPrevItemId(picPosition));
        }
    }); 
}

}

HelloGridView Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hellogridview"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".HelloGridViewActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".PictureViewer"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

</manifest>