我正在尝试在位图上绘制一些文本并将其作为幻灯片的一部分。我一直在遵循本教程:http://www.skoumal.com/en/android-how-draw-text-bitmap/
问题是位图上没有绘制任何内容。我没有收到错误,但是当应该包含文本的两张"幻灯片"通过时,没有任何显示(R.drawable.message_background目前只是白色背景,所以我猜测它在技术上是"显示")。不显示任何文本。这是我的代码:
package midamcorp.com.burgerkingapp;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
public Bitmap drawText(Context c, int resource, String text) {
Resources resources = c.getResources();
Bitmap bitmap = BitmapFactory.decodeResource(resources, resource);
android.graphics.Bitmap.Config config = bitmap.getConfig();
if(config == null) {
config = android.graphics.Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(config, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
float scale = resources.getDisplayMetrics().density;
paint.setColor(Color.BLACK);
paint.setTextSize(18 * scale);
Rect outline = new Rect();
paint.getTextBounds(text, 0, text.length(), outline);
int x = ((bitmap.getWidth() - outline.width() / 2));
int y = ((bitmap.getWidth() + outline.height() / 2));
canvas.drawText(text, x, y, paint);
canvas.save();
return bitmap;
}
ArrayList<Bitmap> imageList = new ArrayList<Bitmap>();
int slideCounter = 0;
ImageView slide;
Cursor images;
final Handler handler = new Handler(Looper.getMainLooper());
private int counter = 0;
private Runnable runnable = new Runnable() {
@Override
public void run() {
slide.setImageBitmap(imageList.get(counter));
if(counter == (imageList.size() - 1)) {
counter = 0;
} else {
counter++;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper dbHelper = new databaseHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
final String store = preferences.getString("store", "");
String managerMessageText = "";
String mainMessageText = "";
try {
File managerFile = new File(this.getFilesDir(), store + ".txt");
File universalFile = new File(this.getFilesDir(), "universal.txt");
BufferedReader reader = new BufferedReader(new FileReader(managerFile));
managerMessageText = reader.readLine();
reader = new BufferedReader(new FileReader(universalFile));
mainMessageText = reader.readLine();
} catch (Exception e) {
Log.e("Error opening file: ", e.getMessage());
}
images = db.rawQuery("SELECT " + databaseHelper.IMAGE + " FROM " + databaseHelper.TABLE_NAME + " where " + databaseHelper.LTO + " = 1", null);
images.moveToFirst();
while(!images.isAfterLast()) {
imageList.add(BitmapFactory.decodeByteArray(images.getBlob(images.getColumnIndex(databaseHelper.IMAGE)), 0, images.getBlob(images.getColumnIndex(databaseHelper.IMAGE)).length ));
images.moveToNext();
}
imageList.add(drawText(this, R.drawable.message_background, "Message from the manager: "));
imageList.add(drawText(this, R.drawable.message_background, "Message from MidAm: "));
slide = (ImageView) findViewById(R.id.imageView);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
updateGUI();
}}, 0, 5000);
};
private void updateGUI() {
handler.post(runnable);
}
}
非常感谢!
我实际上想通了这个问题。以下行中存在一些操作顺序/括号问题:
int x = ((bitmap.getWidth() - outline.width() / 2));
int y = ((bitmap.getWidth() + outline.height() / 2));
应为:
int x = ((bitmap.getWidth() - outline.width() )) / 2;
int y = ((bitmap.getWidth() + outline.height() )) / 2;