机器人 - 如何设置图像的ImageView以正确的比例 [英] android - How to set image in ImageView to right proportions
问题描述
大家好结果
看看下面的图片。结果
我从code belove SD卡加载图像。
我不能让图片自动补ImageView的。我设置的背景颜色为绿色仅仅是为了突出问题。我已经尝试了很多Android:ScaleType像CENTER和FIT_XY但。
林现在使用的inSampleSize = 8,但如何能在ImageView的自动缩放图像是在正确的HIGHT /宽比例和填充屏幕
还有另一件事,你看ExitText和按钮被放置在ImageView的(我认为)以上。
我希望他们能够在不阻止的ImageView。也许我误解了Android布局技术。
DrawView.java
公共类DrawView扩展了ImageView的{私人上下文CTX;
公共DrawView(上下文的背景下,AttributeSet中的ATT){
超(背景下,ATT以);
this.ctx =背景;
}
@覆盖
保护无效的onDraw(帆布油画){
。字符串文件路径= Environment.getExternalStorageDirectory()的toString()+/PTPPservice/163693fe-7c48-4568-a082-00047123b9f1.2.IMAG2200.jpg
BitmapFactory.Options选项=新BitmapFactory.Options();
options.inSampleSize = 8; 位图位图= BitmapFactory.de codeFILE(文件路径,期权); 矩形矩形=新的Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
canvas.drawBitmap(位图,空,矩形,NULL);
}
}
Main1.xml
<?pre>&LT; XML版本=1.0编码=UTF-8&GT;&LT;的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk / RES / Android的
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT&GT;&LT; com.hasse.move.DrawView
机器人:ID =@ + ID / MAINVIEW
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_alignParentTop =真
机器人:背景=#00FF00
机器人:adjustViewBounds =真
/&GT;
&LT; RelativeLayout的
机器人:ID =@ + ID / InnerRelativeLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真正的&GT;
&LT;的EditText
机器人:ID =@ + ID / edittextaddtext
机器人:layout_width =FILL_PARENT
机器人:layout_toLeftOf =@ + ID / btnsave
机器人:layout_height =WRAP_CONTENT
机器人:文字=包装
/&GT;
&LT;按钮
机器人:ID =@ + ID / btnsave
机器人:layout_alignParentRight =真
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=拯救
/&GT;
&LT; / RelativeLayout的&GT;
&LT; / RelativeLayout的&GT;
主要活动
公共类主要活动扩展{ DrawView theImage;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//绘制视图
的setContentView(R.layout.main1); theImage =(DrawView)findViewById(R.id.mainView);
//做的东西
}
@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG)
{
// Toast.makeText(这一点,onConfigurationChanged,Toast.LENGTH_LONG).show();
super.onConfigurationChanged(NEWCONFIG);
}
}
在这里,您正在使用的目标矩形要加载的位图,而不是画布全尺寸的大小:矩形RECT =新矩形(0,0,bitmap.getWidth(),bitmap.getHeight());
您需要找出你的画布的大小。对于您可以使用onSizeChanged:
@覆盖
公共无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
super.onSizeChanged(W,H,oldw,oldh); screenW = W;
screenH = H;
}
hi all
Look at the picture below.
I'm loading an image from sdcard with code belove.
I cannot get the picture to automatically fill the ImageView. I set the background to color green only to highlight the problem. I have tried many android:ScaleType like CENTER and FIT_XY but.
Im now using inSampleSize = 8 but how can the ImageView automatically scale the image to be in the right hight/width proportions and fill the screen
Also another thing, You see the ExitText and Button is placed above the Imageview(I think). I want them to be under and not block the ImageView. Maybe i misunderstand the android layout technical.
DrawView.java
public class DrawView extends ImageView {
private Context ctx;
public DrawView(Context context, AttributeSet atts) {
super(context, atts);
this.ctx = context;
}
@Override
protected void onDraw(Canvas canvas) {
String filePath = Environment.getExternalStorageDirectory().toString() + "/PTPPservice/163693fe-7c48-4568-a082-00047123b9f1.2.IMAG2200.jpg";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(filePath,options);
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawBitmap(bitmap, null, rect,null);
}
}
Main1.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.hasse.move.DrawView
android:id="@+id/mainView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:background="#00FF00"
android:adjustViewBounds="true"
/>
<RelativeLayout
android:id="@+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="@+id/edittextaddtext"
android:layout_width="fill_parent"
android:layout_toLeftOf="@+id/btnsave"
android:layout_height="wrap_content"
android:text="wrap"
/>
<Button
android:id="@+id/btnsave"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
/>
</RelativeLayout>
</RelativeLayout>
Main Activity
public class Main extends Activity {
DrawView theImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// draw the view
setContentView(R.layout.main1);
theImage = (DrawView) findViewById(R.id.mainView);
//do stuff
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
// Toast.makeText(this, "onConfigurationChanged",Toast.LENGTH_LONG).show();
super.onConfigurationChanged(newConfig);
}
}
Here you are using the destination rectangle to be the size of the loaded bitmap instead of the full size of canvas: Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
You need to find out the size of your canvas. For that you can use onSizeChanged:
@Override
public void onSizeChanged (int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
screenW = w;
screenH = h;
}
这篇关于机器人 - 如何设置图像的ImageView以正确的比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!