如何在viewpager应用程序中全屏显示位于drawable文件夹中的图像?

时间:2014-08-23 20:42:48

标签: java android android-viewpager gallery

我开发了一个应用程序,假设使用viewpager向用户显示图像。但我的问题是我的图像没有全屏显示。任何人都可以建议如何使这项工作的代码?以下是我的代码..

Mainactivity.java

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;




public class MainActivity extends Activity {

    MediaPlayer oursong;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
      oursong.start ();
    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImageAdapter adapter = new ImageAdapter(this);
    viewPager.setAdapter(adapter);
  }

  private ShareActionProvider mShareActionProvider;


  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate menu resource file.
      getMenuInflater().inflate(R.menu.activity_main, menu);

      // Locate MenuItem with ShareActionProvider
      MenuItem item = menu.findItem(R.id.menu_item_share);

      // Fetch and store ShareActionProvider
      mShareActionProvider = (ShareActionProvider) item.getActionProvider();

      // Return true to display menu
      return true;
  }

  // Call to update the share intent
  private void setShareIntent(Intent shareIntent) {
      if (mShareActionProvider != null) {
          mShareActionProvider.setShareIntent(shareIntent);
      }
  }

     @Override
     protected void onPause(){
     super.onPause();
      oursong.release();
 }

}

imageadapter.java

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {
    Context context;
    private int[] GalImages = new int[] {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three
    };
    ImageAdapter(Context context){
        this.context=context;
    }
    @Override
    public int getCount() {
      return GalImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
      return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
      ImageView imageView = new ImageView(context);
      int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      imageView.setImageResource(GalImages[position]);
      ((ViewPager) container).addView(imageView, 0);
      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
      ((ViewPager) container).removeView((ImageView) object);
    }
  }

1 个答案:

答案 0 :(得分:0)

如果你真的希望它是全屏的(注意你的资产的分辨率),你应该为你的ImageView使用不同的比例类型:

  imageView.setScaleType(fullscreen ? ImageView.ScaleType.CENTER_CROP : ImageView.ScaleType.CENTER_INSIDE);

这将裁剪您的图像,保持初始比例。

Source