Android - 位图上的难度绘图文本

时间:2016-04-21 15:48:07

标签: java android graphics bitmap

我试图在位图上绘制一些文本并将其作为幻灯片放映的一部分包含在内。我一直在关注本教程:http://www.skoumal.com/en/android-how-draw-text-bitmap/

问题是在位图上没有绘制任何内容。我没有收到任何错误,但是当两个幻灯片出现错误时应该包含文本传递,没有任何显示(R.drawable.message_background目前只是一个白色背景,所以我猜它在技术上是#34;显示")。没有文字出现。这是我的代码:

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);
    }


}

非常感谢你!

1 个答案:

答案 0 :(得分:0)

我实际上已经找到了问题。以下行中有一些操作/括号问题的顺序:

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;