Android Text over image
이미지가있는 imageView가 있으며 해당 이미지 위에 텍스트를 배치하고 싶습니다. 어떻게 할 수 있습니까?
그것이 내가 한 방법이며 RelativeLayout 내부에서 요청한대로 정확하게 작동했습니다.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myImageSouce" />
<TextView
android:id="@+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/myImageView"
android:layout_alignTop="@id/myImageView"
android:layout_alignRight="@id/myImageView"
android:layout_alignBottom="@id/myImageView"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#000000" />
</RelativeLayout>
다른 측면에서 가져갈 수 있습니다. 배경에 드로어 블이있는 TextView를 사용하는 것이 더 쉽습니다.
<TextView
android:id="@+id/text"
android:background="@drawable/rounded_rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</TextView>
당신은 아마도
- Class ImageView에서 상속 된 새 클래스를 만들고
- Method를 재정의하십시오
onDraw
.super.onDraw()
먼저 해당 메서드를 호출 하고 - 그런 다음 표시하려는 텍스트를 그립니다.
이렇게하면 다른 구성 요소와 함께 더 쉽게 레이아웃 할 수있는 단일 레이아웃 구성 요소로 사용할 수 있습니다.
이를 위해 FrameLayout 또는 Merge 레이아웃을 사용하려고합니다. Android 개발자 가이드에는 여기에 대한 훌륭한 예가 있습니다. Android Layout Tricks # 3 : Optimize by merge .
There are many ways. You use RelativeLayout or AbsoluteLayout.
With relative, you can have the image align with parent on the left side for example and also have the text align to the parent left too... then you can use margins and padding and gravity on the text view to get it lined where you want over the image.
You can use a TextView and change its background to the image you want to use
For this you can use only one TextView with android:drawableLeft/Right/Top/Bottom
to position a Image to the TextView. Furthermore you can use some padding between the TextView and the drawable with android:drawablePadding=""
Use it like this:
<TextView
android:id="@+id/textAndImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="@drawable/yourDrawable"
android:drawablePadding="10dp"
android:text="Look at the drawable below"/>
With this you don't need an extra ImageView. It's also possible to use two drawables on more than one side of the TextView.
The only problem you will face by using this, is that the drawable can't be scaled the way of an ImageView.
Try the below code this will help you`
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/gallery1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#7ad7d7d7"
android:gravity="center"
android:text="Juneja Art Gallery"
android:textColor="#000000"
android:textSize="15sp"/>
</RelativeLayout>
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();
}
}
});
}
}
참고URL : https://stackoverflow.com/questions/5242951/android-text-over-image
'program story' 카테고리의 다른 글
angularjs에서 timeInterval을 지우거나 중지하는 방법은 무엇입니까? (0) | 2020.09.07 |
---|---|
딕셔너리 목록을 단일 딕셔너리로 병합하는 방법은 무엇입니까? (0) | 2020.09.07 |
작업 실행시 AWS ECS 오류 : 클러스터에 컨테이너 인스턴스가 없습니다. (0) | 2020.09.07 |
Windows 명령 프롬프트에서 꺾쇠 괄호 이스케이프 (0) | 2020.09.07 |
Breadth-First Search에서 경로를 추적하는 방법은 무엇입니까? (0) | 2020.09.07 |