如何设置图像的大小? [英] How to set size of the image?
本文介绍了如何设置图像的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 splash.png 并希望这一形象采取类似 fitXY
屏幕上所有的地方 ImageView的
。 splash.png 有大小480x767。
什么我必须在我的code键更改?
公共类BitmapConfigView扩展的LinearLayout {
私人位图mImage;
民营涂料mPaint =新的油漆();
公共BitmapConfigView(上下文的背景下){
超(上下文);
BitmapFactory.Options选择采用=新BitmapFactory.Options();
opts.in preferredConfig = Bitmap.Config.ARGB_8888;
opts.inScaled = FALSE;
mImage = BitmapFactory.de codeResource(getResources(),R.drawable.splash,选择采用);
mPaint.setDither(真正的);
setWillNotDraw(假);
setOrientation(垂直);
setGravity(Gravity.BOTTOM);
}
@覆盖
保护无效的OnDraw(帆布油画){
canvas.drawBitmap(mImage,0.0,0.0,mPaint);
}
}
解决方案
这样一来,我用的WebView
:
公共类TestwebViewimageActivity延伸活动{
私人的WebView WV;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
WV =(web视图)findViewById(R.id.webview);
wv.setWebViewClient(新HelloWebViewClient());
wv.setBackgroundColor(00000000);
。wv.getSettings()setBuiltInZoomControls(假);
wv.setVerticalScrollBarEnabled(假);
wv.setHorizontalScrollBarEnabled(假);
wv.loadUrl(文件:///android_asset/6.html);
}
私有类HelloWebViewClient扩展WebViewClient {
@覆盖
公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){
view.loadUrl(URL);
返回true;
}
}
}
的main.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<的WebView
机器人:ID =@ + ID / web视图
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>
< / LinearLayout中>
6.html
< HTML>
< HEAD>
<风格类型=文本/ CSS>
体 {
保证金:0;
背景颜色:黑色;
}
HTML,身体,#我的图像{
宽度:100%;
高度:100%;
}
#我的图像{
背景尺寸:盖;
背景图像:网址(splash.png);
背景位置:中心中心;
背景重复:不重复;
}
< /风格>
< /头>
<身体GT;
< DIV ID =我的形象>< / DIV>
< /身体GT;
< / HTML>
I have splash.png and want that this image take all place on the screen like fitXY
for ImageView
. splash.png has sizes 480x767.
What does I must to change in my code?
public class BitmapConfigView extends LinearLayout {
private Bitmap mImage;
private Paint mPaint = new Paint();
public BitmapConfigView(Context context) {
super(context);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
opts.inScaled = false;
mImage = BitmapFactory.decodeResource( getResources(), R.drawable.splash, opts);
mPaint.setDither(true);
setWillNotDraw(false);
setOrientation(VERTICAL);
setGravity(Gravity.BOTTOM);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mImage, 0.0f, 0.0f, mPaint);
}
}
解决方案
As a result, I used WebView
:
public class TestwebViewimageActivity extends Activity {
private WebView wv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(new HelloWebViewClient());
wv.setBackgroundColor(0x00000000);
wv.getSettings().setBuiltInZoomControls(false);
wv.setVerticalScrollBarEnabled(false);
wv.setHorizontalScrollBarEnabled(false);
wv.loadUrl("file:///android_asset/6.html");
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
6.html
<html>
<head>
<style type="text/css">
body {
margin: 0;
background-color: black;
}
html, body, #my-image {
width: 100%;
height: 100%;
}
#my-image {
background-size: cover;
background-image: url(splash.png);
background-position: center center;
background-repeat: no-repeat;
}
</style>
</head>
<body>
<div id="my-image"></div>
</body>
</html>
这篇关于如何设置图像的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文