问题
I have developed an app that is suppose to display frame on the top of the original image with thw help of SO user.. However, I have small problem .. and the problem is that the some part of the original image gets hidden under the frame.. My question is why does that happen and how can I fix it ?... I want the image to fit the frame ..so that original image appear under the frame..I am new to android so any code help on createScaledBitmap along with explanation is appreciated...
Following is my code..
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>
回答1:
You could put some attributes in your XML for the image. ScaleType determines how the image should be scaled if the view isn't the same size as the image.
<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>
although http://developer.android.com/reference/android/widget/ImageView.ScaleType.html has better detailed info
来源:https://stackoverflow.com/questions/25794570/some-image-parts-get-hidden-under-frame-in-viewpager-imageview