У меня есть TextView в ScrollView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/textAreaScroller"
android:layout_width="400px"
android:layout_height="200px"
android:layout_x="0px"
android:layout_y="25px"
android:fadeScrollbars="false"
android:scrollbarSize="3px"
android:scrollbarStyle="insideOverlay" >
<TextView
android:id="@+id/scrapbook"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="" />
</ScrollView>
<Button
android:id="@+id/upBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Up" />
<Button
android:id="@+id/downBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Down" />
<ImageView
android:id="@+id/imageView"
android:layout_width="400px"
android:layout_height="200px"
/>
</LinearLayout>
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), растровое изображение не отражает текущее содержимое TextView, а вместо этого всегда имеет только начальное содержимое TextView (неважно, какой текущее положение прокрутки). Я обновил сообщение, чтобы предоставить рабочий код - возможно, он будет работать на другом устройстве sb.
ОБНОВЛЕНИЕ
Я также пытался проверить идею WarrenFaith, переопределив onDraw
в моем пользовательском TextView, но каким-то образом он по-прежнему рисует только начальное содержимое 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);
}
}