【问题标题】:Some image parts get hidden under frame in viewpager/imageview?某些图像部分隐藏在 viewpager/imageview 的框架下?
【发布时间】:2014-09-11 18:24:55
【问题描述】:

我开发了一个应用程序,假设在 SO 用户的帮助下在原始图像的顶部显示框架。但是,我有一个小问题 .. 问题是原始图像的某些部分得到了隐藏在框架下..我的问题是为什么会发生这种情况,我该如何解决?...我希望图像适合框架..以便原始图像出现在框架下..我是 android 新手所以任何对 createScaledBitmap 的代码帮助以及解释表示赞赏...

以下是我的代码..

    import android.content.Context;
    import android.content.res.Resources;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.graphics.drawable.LayerDrawable;
    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,
            R.drawable.four,
            R.drawable.five
        };
        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_small);
          imageView.setPadding(padding, padding, padding, padding);
          imageView.setScaleType(ImageView.ScaleType.FIT_XY);
          Resources r = context.getResources();
          Bitmap bmp = BitmapFactory.decodeResource(r, GalImages[position]);
          int width=200;
          int height=200;
          Bitmap resizedbitmap = Bitmap.createScaledBitmap(bmp, width, height, true);
          Drawable d = new BitmapDrawable(r,resizedbitmap);
          Drawable[] layers = new Drawable[2];
          layers[0] = d;
          layers[1] = r.getDrawable(R.drawable.a);
          LayerDrawable layerDrawable = new LayerDrawable(layers);
          imageView.setImageDrawable(layerDrawable);
          ((ViewPager) container).addView(imageView, 0);
          return imageView;
        } 

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

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:icon="@drawable/icon" >



          <android.support.v4.view.ViewPager
          android:id="@+id/view_pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
         android:icon="@drawable/icon"
          />
           <ImageView
        android:id="@+id/swipe_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/swipe_left" />

    <ImageView
        android:id="@+id/swipe_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/swipe_right" />

</RelativeLayout>

【问题讨论】:

    标签: android android-viewpager gallery android-imageview


    【解决方案1】:

    您可以在图像的 XML 中添加一些属性。 ScaleType 确定在视图与图像大小不同时应如何缩放图像。

           <ImageView
            android:id="@+id/someImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="-6dp"
            android:layout_marginTop="-6dp"
            android:background="@drawable/dropshadow2"
            android:rotation="-6"
            android:scaleType="fitCenter">                <---- either in XML or code
           </ImageView>
    

    虽然http://developer.android.com/reference/android/widget/ImageView.ScaleType.html 有更好的详细信息

    【讨论】:

    • @Martin..抱歉没有提供activity_main.xml
    • 将最后一行 - android:scaleType="fitCenter" 放入您的 ImageView。看看这是否不能使图像完全适合
    • 尝试将您的浏览器高度设置为小于“match_parent”的值。它将使用整个屏幕并且不会为您的图像留出空间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多