2 newcichang newcichang 于 2013.11.09 21:27 提问

为什么画出来的矩形宽度不能沾满屏幕

package com.userself.userselfview.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

public class TextchangeView extends ImageView {
private Paint mPaint = new Paint();//画笔
public Bitmap b = null;
public TextchangeView(Context context, AttributeSet attrs) {
super(context,attrs);
}
public void setTextImageView(String text){
// 抗锯齿的方法 平滑
mPaint.setAntiAlias(true);
//该方法用于设置画笔的空心线宽。该方法在矩形、圆形等图形上有明显的效果
mPaint.setStrokeWidth(3);
int iconWidth=this.getWidth();
Log.e("test","宽度为"+this.getWidth());
if(iconWidth<=0){
iconWidth = 480;
}
int iconHeigth=this.getHeight();
Log.e("test","高度为"+this.getHeight());
if (iconHeigth<=0){
iconHeigth = 800;
}
if(b == null){
// 制定图片的宽和高创建新位图
b = Bitmap.createBitmap(iconWidth, iconHeigth, Bitmap.Config.ARGB_8888);
}
mPaint.setColor(Color.BLACK);
Canvas canvas = new Canvas(b);//传位图参数做背景
//画矩形
canvas.drawRect(0, 0,iconWidth, iconHeigth, mPaint);
Log.e("test","画笔密度"+canvas.getDensity());
this.setImageBitmap(b);

}

}

package com.userself.userselfview;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;

import com.userself.userselfview.view.TextchangeView;

public class MainActivity extends Activity {
private TextchangeView textchangeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textchangeView = (TextchangeView)findViewById(R.id.textchangeview);

    textchangeView.setTextImageView("ABCD");



    DisplayMetrics metric = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metric);
    int width = metric.widthPixels;
    int higth = metric.heightPixels;

    float density = metric.density;


    Log.e("test","width==="+width+"===hight==="+higth+"==========="+density);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<com.userself.userselfview.view.TextchangeView

    android:id="@+id/textchangeview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/red"
    />

在480*800的分辨率 的手机上测试,画出来的图片的高度沾满了屏幕,而图片的宽度没有沾满屏幕,不知道原因是什么?

1个回答

ll_0520
ll_0520   2013.11.10 11:29

看下AndroidManifest.xml中应用或该Activity的theme有没有设置Padding之类的

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!