在Android应用程序的教程使图像全屏 [英] Making image full screen on android tutorial app

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

问题描述

使用你好,世界的GridView教程例如,我试图使上点击而不是显示图像的阵列中的位置的图像全屏。由于我unfamilar与Android,这是我第一次尝试开发它,我不知所措。我熟悉Java虽然,我试着做这样的事情(不显着的工作):

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);
    gridview.setAdapter(新ImageAdapter(本));

    gridview.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
            showImage(V,位置);
            Toast.makeText(HelloAndroid.this,+ parent.getId(),Toast.LENGTH_SHORT).show();
        }
    });
}

私人无效showImage(查看视图,INT位置){
    ImageView的imgView =(ImageView的)观点;
    imgView.setImageResource(位置);
}
 

但应用程序崩溃(强制关闭)。任何想法?

下面是本教程的应用程序,我使用

解决方案

  imgView.setImageResource(位置);
 

这给了你,因为你不能正确使用它的错误。该参数应指向图像的资源ID(它是一个int)如 R.id.imageid 。即使您提供正确的资源ID,你要怎么做是行不通的。

要得到正确的资源ID,你就需要调用视图的适配器,并使用getItemId(位置)方法以获得正确的资源ID。在你的ImageAdapter,如果你改变你的getItemId方法返回mThumbsIds [位置],而不是0(如Google网上的例子)

不过,要实现你的结果,我会建议为你创建一个新的活动,只是加载图像之类的。

例如:

  gridview.setOnItemClickListener(新OnItemClickListener(){
    公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
        长imageId =(ImageAdapter)parent.getAdapter.getItemAt(位置);

        意图fullScreenIntent =新的意图(v.getContext(),FullScreenImage.class);
        fullScreenIntent.putExtra(thisClassName.class.getName(),imageId);

        thisClassName.this.startActivity(fullScreenIntent);

    }
});
 

假设你创建一个full_image.xml布局文件,只包含一个ImageView的以fullImage的ID,你的FullScreenImage类应该是这样的:

 公共类FullScreenImage扩展活动
{
  保护无效的onCreate(包savedInstanceState){
   的setContentView(R.layout.full_image);
   意向意图= getIntent();
   长imageId = intent.getExtras()得到(thisClassName.class.getName())。
   ImageView的ImageView的=(ImageView的)v.findViewById(R.id.fullImage);

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

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

   }
 }
 

东西沿着这些路线应该很好地帮助你,有一些调整。请确保您在AndroidManifest.xml文件添加FullScreenImage作为以及。

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?

Here is the tutorial app I am using

解决方案

imgView.setImageResource(position); 

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.

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 goolge's example)

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

for example:

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

    }
});

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

   }
 }

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

这篇关于在Android应用程序的教程使图像全屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆