我已经开发了是假设在原始图像与THW帮助顶部显示框应用程序以便用户。但是,我有个小问题..和问题是原始图像的某些部分得到框架下隐藏..我的问题是,为什么这种情况发生,我怎么能解决这个问题?......我想图像以适应框架..所以说原始图像出现在了frame..I是新到Android所以任何在createScaledBitmap code帮助,解释和说明是AP preciated ...
以下是我的code ..
进口android.content.Context; 进口android.content.res.Resources; 进口android.graphics.Bitmap; 进口android.graphics.BitmapFactory; 进口android.graphics.drawable.BitmapDrawable; 进口android.graphics.drawable.Drawable; 进口android.graphics.drawable.LayerDrawable; 进口android.support.v4.view.PagerAdapter; 进口android.support.v4.view.ViewPager; 进口android.view.View; 进口android.view.ViewGroup; 进口android.widget.ImageView; 公共类ImageAdapter扩展PagerAdapter { 上下文语境; 私人诠释[] = GalImages新INT [] { R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five }; ImageAdapter(上下文的背景下){ this.context =背景; } @覆盖 公众诠释的getCount(){ 返回GalImages.length; } @覆盖 公共布尔isViewFromObject(查看视图,Object对象){ 返回查看==((ImageView的)对象); } @覆盖 公共对象instantiateItem(ViewGroup中的容器,INT位置){ ImageView的ImageView的=新ImageView的(上下文); INT填充= context.getResources()getDimensionPixelSize(R.dimen.padding_small)。 imageView.setPadding(填充,填充,填充,填充); imageView.setScaleType(ImageView.ScaleType.FIT_XY); 资源R = context.getResources(); BMP位图= BitmapFactory.de codeResource(R,GalImages [位置]); INT宽度= 200; INT高= 200; 位图resizedbitmap = Bitmap.createScaledBitmap(BMP,宽度,高度,真正的); 可绘制D =新BitmapDrawable(R,resizedbitmap); 可绘制[] =层新绘制对象[2]; 层[0] = D组; 层[1] = r.getDrawable(R.drawable.a); LayerDrawable layerDrawable =新LayerDrawable(层); imageView.setImageDrawable(layerDrawable); ((ViewPager)容器).addView(ImageView的,0); 返回ImageView的; } @覆盖 公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象){ ((ViewPager)容器).removeView((ImageView的)对象); } }
activity_main.xml中
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android 的xmlns:工具=http://schemas.android.com/tools 机器人:layout_width =match_parent 机器人:layout_height =match_parent 工具:上下文=。MainActivity 机器人:图标=@绘制/图标> < android.support.v4.view.ViewPager 机器人:ID =@ + ID / view_pager 机器人:layout_width =match_parent 机器人:layout_height =match_parent 机器人:图标=@绘制/图标 /> < ImageView的 机器人:ID =@ + ID / swipe_left 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:layout_alignParentLeft =真 机器人:layout_centerVertical =真 机器人:SRC =@绘制/ swipe_left/> < ImageView的 机器人:ID =@ + ID / swipe_right 机器人:layout_width =WRAP_CONTENT 机器人:layout_height =WRAP_CONTENT 机器人:layout_alignParentRight =真 机器人:layout_centerVertical =真 机器人:SRC =@绘制/ swipe_right/>< / RelativeLayout的>
解决方案
您可以把一些属性在XML的图像。 ScaleType确定如何如果视图不是大小作为图像相同的图像应该被缩放。
< ImageView的 机器人:ID =@ + ID / someImage 机器人:layout_width =match_parent 机器人:layout_height =match_parent 机器人:layout_marginLeft = - 6DP 机器人:layout_marginTop = - 6DP 机器人:背景=@绘制/ dropshadow2 机器人:旋转= - 6 机器人:scaleType =fitCenter> < ----无论是在XML或code < / ImageView的>
http://developer.android.com/reference/android /widget/ImageView.ScaleType.html 具有更好的详细信息,
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>
解决方案
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