如何在Android中使用scrollview将字符串转换为位图图像
我只想在我的editextbox中创建任何数据的图像。它可能有30行或40行或更多,然后将其转换为图像并将该图像发送给其他活动 /////////////XML文件/////////////////如何在Android中使用scrollview将字符串转换为位图图像,android,Android,我只想在我的editextbox中创建任何数据的图像。它可能有30行或40行或更多,然后将其转换为图像并将该图像发送给其他活动 /////////////XML文件///////////////// <LinearLayout android:id="@+id/mainlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:lay
<LinearLayout
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/line"
android:layout_below="@+id/line2"
android:background="#FFFFFF"
android:gravity="center">
<ScrollView
android:id="@+id/scroll_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:cacheColorHint="@android:color/transparent"
android:fadingEdge="none"
android:fadingEdgeLength="0dp"
android:gravity="center"
android:scrollbarStyle="insideOverlay"
android:scrollbars="none"
android:fillViewport="true"
android:scrollingCache="false">
<EditText
android:id="@+id/edt_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="@android:color/transparent"
android:cursorVisible="true"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:hint="..."
android:inputType="textMultiLine"
android:padding="3dp"
android:paddingBottom="10dp"
android:textSize="@dimen/font_size"
android:windowSoftInputMode="stateHidden"
android:maxLines="50">
<requestFocus/>
</EditText>
</ScrollView>
</LinearLayout>
img_ok = (Button) findViewById(R.id.img_ok);
img_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*anstxt = edt_text.getText().toString();
butnlyt.setVisibility(View.GONE);
View view = findViewById(R.id.mainlayout);
view.refreshDrawableState();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(getApplicationContext(),PreviewActivity.class);
in1.putExtra("image", byteArray);
startActivity(in1);
finish();*/
edt_text.setCursorVisible(false);
edt_text.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(edt_text.getDrawingCache());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(getApplicationContext(),PreviewActivity.class);
in1.putExtra("image", byteArray);
startActivity(in1);
finish();
}
});
以下是将字符串转换为位图的过程,但字符串应使用Base64编码
/**
* @param encodedString
* @return bitmap (from given string)
*/
public Bitmap StringToBitMap(String encodedString){
try{
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}