在android教程应用程序上制作图像全屏

Making image full screen on android tutorial app(在android教程应用程序上制作图像全屏)

本文介绍了在android教程应用程序上制作图像全屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Hello, World Gridview 教程示例,我试图在单击时使图像全屏显示,而不是显示图像在数组中的位置.由于我对 Android 不熟悉,这是我第一次尝试使用它进行开发,所以我很茫然.不过,我对 Java 很熟悉,并且我尝试过这样做(这显然不起作用):

Using the Hello, World Gridview tutorial example, I am attempting to make the image fullscreen on click instead of display the position of the image in the array. As I am unfamilar with Android and this is my first development attempt with it, I'm at a loss. I am familiar with Java though, and I've tried doing things like this (that don't work obviously):

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

    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) {
            showImage(v, position);
            Toast.makeText(HelloAndroid.this, "" + parent.getId(), Toast.LENGTH_SHORT).show();
        }
    });
}

private void showImage(View view, int position) {
    ImageView imgView = (ImageView) view; 
    imgView.setImageResource(position);     
}

但应用程序崩溃(强制关闭).有什么想法吗?

But the app crashes (force close). Any ideas?

这是我正在使用的教程应用

推荐答案

imgView.setImageResource(position); 

这会给您一个错误,因为您没有正确使用它.该参数应指向图像的资源 ID(它是一个 int),例如R.id.imageid.即使您要提供正确的资源 ID,您想要做的也不会奏效.

This gives you an error because you are not using it correctly. The parameter should be pointing to the resource ID of the image (which is an int) e.g. R.id.imageid. Even if you were to supply the right resource ID, what you want to do would not work.

要获取正确的资源 ID,您需要调用视图的适配器并使用 getItemId(position) 方法来获取正确的资源 ID.在您的 ImageAdapter 中,如果您将 getItemId() 方法更改为返回 mThumbsIds[position] 而不是 0(如 Google 的示例中所示).

To get the right resource ID, you would need to call the adapter of the view and use the getItemId(position) method to get the correct resource ID. in your ImageAdapter, if you change your getItemId() method to return mThumbsIds[position] rather than 0 (as in Google's example).

但是,为了实现您的结果,我建议您创建一个新 Activity 并像这样加载图像.

However, to achieve your result I would suggest for you to create a new Activity and just load the image like that.

例如:

gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        long imageId = (ImageAdapter)parent.getAdapter.getItemAt(position);

        Intent fullScreenIntent = new Intent(v.getContext(), FullScreenImage.class);
        fullScreenIntent.putExtra(thisClassName.class.getName(), imageId);

        thisClassName.this.startActivity(fullScreenIntent);
    }
});

假设您创建了一个 full_image.xml 布局文件,其中只包含一个 ID 为 "fullImage" 的 ImageView,您的 FullScreenImage 类应该如下所示:

Assuming you create a full_image.xml layout file, containing just an ImageView with an id of "fullImage", your FullScreenImage class should look like this:

public class FullScreenImage extends Activity
{
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.full_image);
        Intent intent = getIntent();
        long imageId = intent.getExtras().get(thisClassName.class.getName());
        ImageView imageView = (ImageView) v.findViewById(R.id.fullImage);

        imageView.setLayoutParams( new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));

        imageView.setImageResource(imageId);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    }
}

这些方面的东西应该适合你,并进行一些调整.确保在 AndroidManifest.xml 文件中也将 FullScreenImage 添加为 <activity>.

Something along these lines should work well for you, with some tweaking. Make sure you add FullScreenImage in your AndroidManifest.xml file as an <activity> as well.

这篇关于在android教程应用程序上制作图像全屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在android教程应用程序上制作图像全屏

基础教程推荐