Jak narysować tekst na obrazku?
Chcę narysować tekst na obrazku (aby zapisać obrazek z tekstem ). mam widok obrazu ustawiłem bitmapę na ten obrazek chcę narysować tekst na obrazku (tekst wprowadzony przez użytkownika ). próbowałem tego przed oszczędzaniem.....
void saveImage() {
File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
originalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Kod Xml jest..
<FrameLayout
android:id="@+id/framelayout"
android:layout_marginTop="30dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageView
android:id="@+id/ImageView01"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView android:id="@+id/text_view2"
android:layout_marginTop="20dip"
android:layout_width="wrap_content"
android:text="SampleText"
android:textSize="12pt"
android:layout_alignTop="@+id/ImageView01"
android:layout_height="wrap_content"/>
</FrameLayout>
6 answers
Zaktualizowano metodę SaveImage () do obsługi rysowania tekstu.
void saveImage() {
File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
// NEWLY ADDED CODE STARTS HERE [
Canvas canvas = new Canvas(originalBitmap);
Paint paint = new Paint();
paint.setColor(Color.WHITE); // Text Color
paint.setTextSize(12); // Text Size
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern
// some more settings...
canvas.drawBitmap(originalBitmap, 0, 0, paint);
canvas.drawText("Testing...", 10, 10, paint);
// NEWLY ADDED CODE ENDS HERE ]
originalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Daj mi znać, jeśli to zadziała.
Shash
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-12 16:31:29
Jako zasugerował Vladislav Skoumal , wypróbuj tę metodę:
public Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) {
try {
Resources resources = mContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
if(bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
// resource bitmaps are imutable,
// so we need to convert it to mutable one
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
// new antialised Paint
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
paint.setColor(Color.rgb(110,110, 110));
// text size in pixels
paint.setTextSize((int) (12 * scale));
// text shadow
paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(mText, 0, mText.length(), bounds);
int x = (bitmap.getWidth() - bounds.width())/6;
int y = (bitmap.getHeight() + bounds.height())/5;
canvas.drawText(mText, x * scale, y * scale, paint);
return bitmap;
} catch (Exception e) {
// TODO: handle exception
return null;
}
}
Wywołanie tej metody
Bitmap bmp =drawTextToBitmap(this,R.drawable.aa,"Hello Android");
img.setImageBitmap(bmp);
Wyjście
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-30 03:24:07
Można rozszerzyć widok, aby utworzyć własny widok. Coś jak
public class PieView extends View {
public PieView(Context context) {
super(context);
overlayBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.piechart_shade,
null);
overlayWidth = overlayBitmap.getWidth();
setLayoutParams(new LayoutParams(overlayWidth, overlayWidth));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
W metodzie ondraw można użyć canvas.drawBitmap i canvas.drawText do rysowania bitmap i tekstu.
W ten sposób nie wymagasz FrameLayout, ponieważ wszystko jest w jednym widoku niestandardowym.
Możesz to umieścić w pliku xml jako
<com.raj.PieView android:id="@+id/framelayout" android:layout_marginTop="30dip"
android:layout_height="fill_parent" android:layout_width="fill_parent"/>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-09-06 13:56:26
- Utwórz pustą bitmapę
- Utwórz nowy obiekt Canvas i przekaż do niego tę bitmapę
- widok wywołania.narysuj (Canvas) przekazując obiekt canvas, który właśnie utworzyłeś. Szczegółowe informacje można znaleźć w dokumentacji metody.
- Użyj Bitmapy.compress (), aby zapisać zawartość bitmapy do strumienia wyjściowego, może pliku.
Pseudo kod:
Bitmap bitmap = Bitmap.createBitmap(200,200,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText();
//necessary arguments and draw whatever you want. thes all are drawn on the bitmap.finally save this bitmap
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-09-06 14:29:01
Nadmuchaj widok tekstowy nad obrazem . Refer http://www.android10.org/index.php/forums/43-view-layout-a-resource/715-tutorial-android-xml-view-inflation dla podstawowego przykładu. To powinien być najprostszy sposób.
LinearLayout lLayout;
lLayout = (LinearLayout)findViewById(R.id.layout1);
layout1 is the main layout.
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView tv = (TextView)inflater.inflate(R.layout.text, null);
lLayout.addView(tv);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-14 06:29:01
Rozwiązuję ten problem (plik niezmienny), ale nic nie jest zapisywane w pliku... postępuj zgodnie z moim kodem: public static File writeOnImage (File file) throws IOException {
Bitmap originalBitmap = BitmapFactory.decodeFile(file.getPath());
originalBitmap = convertToMutable(originalBitmap);
FileOutputStream out = new FileOutputStream(file);
try {
Canvas canvas = new Canvas(originalBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(12);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
canvas.drawBitmap(originalBitmap, 0, 0, paint);
canvas.drawText("Testing...", 10, 10, paint);
originalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-07 04:30:25