通过设置Ellipsize在画布上绘制时无法截断TextView

时间:2012-07-19 13:39:25

标签: android android-layout canvas

在Canvas上绘制Textview时遇到问题。我想文本将被截断将超载高度。我设置textView.setEllipsize(TextUtils.TruncateAt.MIDDLE)但它对我不起作用。

这是我的代码

 if(mBitmapNormalImage != null)
            mBitmapNormalImage.recycle();
        mBitmapNormalImage = BitmapFactory.decodeResource(getResources(), bgImageIds[position]);

    if(bmOverlayNormalImage != null)
        bmOverlayNormalImage.recycle();
    bmOverlayNormalImage = Bitmap.createBitmap(mBitmapNormalImage.getWidth(), mBitmapNormalImage.getHeight(),
        Bitmap.Config.ARGB_8888);

    canvasNormalImage = new Canvas(bmOverlayNormalImage);
    canvasNormalImage.drawBitmap(mBitmapNormalImage, 0, 0, null);

    coordTopLeftX = coordinateValues[position][0]*canvasNormalImage.getWidth()/320 ;
    coordTopLeftY = coordinateValues[position][1]*canvasNormalImage.getHeight()/320;
    RectWidth = coordinateValues[position][2]*canvasNormalImage.getWidth()/320;
    RectHeight = coordinateValues[position][3]*canvasNormalImage.getHeight()/320;


    LinearLayout layout = new LinearLayout(this);
    layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));


    TextView textView = new TextView(layout.getContext());
    textView.setVisibility(View.VISIBLE);
    textView.setGravity(Gravity.CENTER);    
    textView.setEllipsize(TextUtils.TruncateAt.MIDDLE);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1f);

    params.gravity=Gravity.CENTER;

    textView.setLayoutParams(params);
 textView.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/"+fonts[position]));
        textView.setText(message);
        textView.setTextColor(Color.argb(colorValue[position][0],colorValue[position][1], colorValue[position][2], colorValue[position][3]));
        textView.setTextSize(TextSize.listSize[position]+15);

    layout.addView(textView);

    layout.measure(MeasureSpec.makeMeasureSpec((int)RectWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((int)RectHeight, MeasureSpec.EXACTLY));
    layout.layout(0, 0, (int)RectWidth, (int)RectHeight);

    // To place the text view somewhere specific:
    canvasNormalImage.translate(coordTopLeftX, coordTopLeftY);

    layout.draw(canvasNormalImage);

    // set the bitmap into the ImageView
    bgImage.setBackgroundDrawable(new BitmapDrawable(getResources(),bmOverlayNormalImage));

0 个答案:

没有答案