将TextView显示为ImageView以启用缩放

时间:2018-05-02 09:09:32

标签: android

我有一个包含动态文本(不同长度)的TextView。为了实现平滑的捏合和缩放,我想将此TextView转换为ImageView并显示ImageView而不是TextView。所以我尝试设置TextView程序,就像这样:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.song_view);

    String text = getIntent().getStringExtra(SongbookListFragment.SONG_TEXT_ARGS);

    song_textview = new AppCompatTextView(this);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    song_textview.setLayoutParams(layoutParams);
    song_textview.setPadding(PADDING,PADDING,PADDING,PADDING);
    song_textview.setText(Html.fromHtml(text));
    song_textview.setTextColor(Color.BLACK);
    song_textview.setBackgroundColor(Color.TRANSPARENT);
    song_textview.setHorizontallyScrolling(true);
    song_textview.setTextSize(16);
}

我的song_view.xml使用扩展ImageView的可缩放PhotoView:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
    <com.github.chrisbanes.photoview.PhotoView
        android:id="@+id/imageview_songtext"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</ScrollView>

现在在活动的onWindowFocusChanged方法中,我将TextView转换为Bitmap并在PhotoView中显示它,如下所示:

@Override
public void onWindowFocusChanged(boolean hasFocus){
    super.onWindowFocusChanged(hasFocus);

    int width = this.getWindow().getDecorView().getWidth();
    int height = this.getWindow().getDecorView().getHeight();
    Bitmap bitmapHolder = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmapHolder);
    if (song_textview != null) {
        song_textview.layout(0, 0, width, height);
        song_textview.draw(c);
        PhotoView songPhotoView = findViewById(R.id.imageview_songtext);
        songPhotoView.setImageBitmap(bitmapHolder);
    }
}

除非我不知道如何在onWindowFocusChanged内设置高度的位图,否则效果很好。如果我在上面的代码示例中使用this.getWindow().getDecorView().getHeight(),则会删除文本的长度,因为this.getWindow().getDecorView().getHeight()会返回屏幕高度而不是TextView的实际高度。当然,TextView没有添加到布局中,这就是没有测量​​高度的原因。那么如何才能获得TextView的全长以在我的Bitmap中显示它?

1 个答案:

答案 0 :(得分:3)

只需调用以下setText方法即可。

public void setText(String text){
            Paint paint = new Paint();
            paint.setTextSize(30);
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.FILL);
            Rect result = new Rect();
            paint.getTextBounds(text, 0, text.length(), result);
            int width=  result.width();
            //background color red
            int color=Color.RED;
            Bitmap img=drawText(text, width, color);
            songPhotoView.setImageBitmap(img);
            //background transparent
            int colorT=Color.TRANSPARENT;
            Bitmap img1=drawText(text,width,colorT);
            songPhotoView.setImageBitmap(img1);
        }

        public Bitmap drawText(String text, int textWidth, int color) {
            // Get text dimensions
            TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
            textPaint.setStyle(Paint.Style.FILL);
            textPaint.setColor(Color.parseColor("#ff00ff"));
            textPaint.setTextSize(30);
            StaticLayout mTextLayout = new StaticLayout(text, textPaint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
            // Create bitmap and canvas to draw to
            Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Bitmap.Config.ARGB_4444);
            Canvas c = new Canvas(b);
            // Draw background
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(color);
            c.drawPaint(paint);
            // Draw text
            c.save();
            c.translate(0, 0);
            mTextLayout.draw(c);
            c.restore();
            return b;
        }