Android Text over image

2019-01-10 03:51发布

I have an imageView with an image, over that image I want to place a text. How can I achieve that?

9条回答
爷的心禁止访问
2楼-- · 2019-01-10 04:36

You could possibly

  • make a new class inherited from the Class ImageView and
  • override the Method onDraw. Call super.onDraw() in that method first and
  • then draw some Text you want to display.

if you do it that way, you can use this as a single Layout Component which makes it easier to layout together with other components.

查看更多
时光不老,我们不散
3楼-- · 2019-01-10 04:37

You may want to take if from a diffrent side: It seems easier to have a TextView with a drawable on the background:

 <TextView
            android:id="@+id/text"
            android:background="@drawable/rounded_rectangle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        </TextView>
查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-10 04:39

The below code this will help you

public class TextProperty {
private int heigt;                              //读入文本的行数
private String []context = new String[1024];    //存储读入的文本

/*
 *@parameter wordNum
 *
 */
public TextProperty(int wordNum ,InputStreamReader in) throws Exception {
    int i=0;
    BufferedReader br = new BufferedReader(in);
    String s;
    while((s=br.readLine())!=null){
        if(s.length()>wordNum){
            int k=0;
            while(k+wordNum<=s.length()){
                context[i++] = s.substring(k, k+wordNum);
                k=k+wordNum;
            }
            context[i++] = s.substring(k,s.length());
        }
        else{
            context[i++]=s;
        }
    }
    this.heigt = i;
    in.close();
    br.close();
}


public int getHeigt() {
    return heigt;
}

public String[] getContext() {

    return context;
}

}

public class MainActivity extends AppCompatActivity {

private Button btn;
private ImageView iv;
private final int WORDNUM = 35;  //转化成图片时  每行显示的字数
private final int WIDTH = 450;   //设置图片的宽度
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iv = (ImageView) findViewById(R.id.imageView);
    btn = (Button) findViewById(R.id.button);

    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int x=5,y=10;
            try {
                TextProperty tp = new TextProperty(WORDNUM, new InputStreamReader(getResources().getAssets().open("1.txt")));
                Bitmap bitmap = Bitmap.createBitmap(WIDTH, 20*tp.getHeigt(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                Paint paint = new Paint();
                paint.setColor(Color.WHITE);
                paint.setTextAlign(Paint.Align.LEFT);
                paint.setTextSize(20f);

                String [] ss = tp.getContext();
                for(int i=0;i<tp.getHeigt();i++){
                    canvas.drawText(ss[i], x, y, paint);
                    y=y+20;
                }

                canvas.save(Canvas.ALL_SAVE_FLAG);
                canvas.restore();
                String path = Environment.getExternalStorageDirectory() + "/image.png";
                System.out.println(path);
                FileOutputStream os = new FileOutputStream(new File(path));
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
                //Display the image on ImageView.
                iv.setImageBitmap(bitmap);
                iv.setBackgroundColor(Color.BLUE);
                os.flush();
                os.close();
            }
            catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    });
}

}

查看更多
登录 后发表回答