android textview绘制,android – 将当前TextView内容绘制到Bitmap

在Android应用中,作者遇到一个问题:当在ScrollView内的TextView滚动时,尝试将TextView的内容绘制到Bitmap,但Bitmap始终只显示TextView的初始内容,而非当前可见的部分。作者尝试了不同的解决方案,包括在触摸事件中更新Bitmap和在自定义TextView中覆盖onDraw方法,但问题仍未解决。博客中提供了相关的代码示例以展示问题所在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在ScrollView中有一个TextView:

TextView有很多文本,这就是可滚动的原因.我需要在TextView中将当前可见内容绘制到Bitmap.出于测试目的,我在ImageView中显示此位图.我有以下代码:

public class TextviewToImageActivity extends Activity {

private TextView textView;

private ScrollView textAreaScroller;

private ImageView imageView;

private Handler mHandler;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mHandler = new Handler();

imageView = (ImageView) findViewById(R.id.imageView);

textAreaScroller = (ScrollView) findViewById(R.id.textAreaScroller);

textView = (TextView) findViewById(R.id.scrapbook);

textView.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v,MotionEvent event) {

imageView.setImageBitmap(loadBitmapFromView(textAreaScroller));

return false;

}

});

Button upBtn = (Button) findViewById(R.id.upBtn);

Button downBtn = (Button) findViewById(R.id.downBtn);

upBtn.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v,MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

scheduleScroller(upScroller);

imageView.setImageBitmap(loadBitmapFromView(textView));

} else if (event.getAction() == MotionEvent.ACTION_UP) {

mHandler.removeMessages(1);

}

return true;

}

});

downBtn.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v,MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

scheduleScroller(downScroller);

imageView.setImageBitmap(loadBitmapFromView(textView));

} else if (event.getAction() == MotionEvent.ACTION_UP) {

mHandler.removeMessages(1);

}

return true;

}

});

loadDoc();

}

private Runnable downScroller = new Runnable() {

public void run() {

textAreaScroller.scrollBy(0,10);

scheduleScroller(downScroller);

}

};

private Runnable upScroller = new Runnable() {

public void run() {

textAreaScroller.scrollBy(0,-10);

scheduleScroller(upScroller);

}

};

private void scheduleScroller(Runnable scrollerJob) {

Message msg = Message.obtain(mHandler,scrollerJob);

msg.what = 1;

mHandler.sendMessageDelayed(msg,10);

}

private static Bitmap loadBitmapFromView(View v) {

Bitmap b = Bitmap.createBitmap(400,200,Bitmap.Config.ARGB_8888);

Canvas c = new Canvas(b);

v.draw(c);

return b;

}

private void loadDoc() {

String s = "";

for (int x = 0; x <= 100; x++) {

s += "Line: " + String.valueOf(x) + "\n";

}

textView.setText(s);

textView.setMovementMethod(new ScrollingMovementMethod());

}

}

问题是,一旦我滚动TextView(触发TouchEvent),Bitmap不会反映TextView的当前内容,而是始终只有TextView的开头内容(当前滚动位置无关紧要).我更新了帖子以提供工作代码 – 也许它可以在sb的其他设备上运行.

UPDATE

我还尝试通过在我的自定义TextView中覆盖onDraw来检查WarrenFaith的想法,但它仍然只是绘制TextView的开头内容:

public class MyTextView extends TextView {

private Bitmap mBitmap;

public MyTextView(Context context,AttributeSet attrs) {

super(context,attrs);

}

public MyTextView(Context context) {

super(context);

}

public MyTextView(Context context,AttributeSet attrs,int defStyle) {

super(context,attrs,defStyle);

}

public Bitmap getBitmap() {

return mBitmap;

}

@Override

protected void onDraw(Canvas canvas) {

mBitmap = Bitmap.createBitmap(canvas.getWidth(),canvas.getHeight(),Bitmap.Config.ARGB_8888);

Canvas c = new Canvas(mBitmap);

super.onDraw(canvas);

super.onDraw(c);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值