gpt4 book ai didi

android - 在 ImageView 上设置文本

转载 作者:搜寻专家 更新时间:2023-11-01 08:13:55 25 4
gpt4 key购买 nike

我有一个 android 应用程序,我在其中用照相手机拍照...然后将照片放在 ImageView 中。接下来我想做的是在它的底部设置一个文本图片。

到目前为止,我已经做了类似的事情:

   ImageView image;
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
image.setImageBitmap(bitmap);

你能帮我告诉我下一步该怎么做吗?一些代码会很好!

编辑:XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<Button
android:id="@+id/logoButton"
android:layout_height="wrap_content"
android:text="LogoButton"
android:layout_marginLeft="40dip"
android:layout_width="224dp">
</Button>

<ImageView
android:id="@+id/imageview"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ImageView>

更新:这是我使用相机的方式:

   ....onCreate(){
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image=new File(Environment.getExternalStorageDirectory(),"PhotoContest.jpg");
camera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(image));
imageUri=Uri.fromFile(image);
startActivityForResult(camera,1);
}

public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){

case 1:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
image= (ImageView) findViewById(R.id.imageview);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
image.setImageBitmap(bitmap);
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
else

if(resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(EditPhoto.this, "Picture could not be taken.", Toast.LENGTH_SHORT).show();
}
}
}

1.我不确定 FrameLayout 的这个技巧是否会在我的图像上放置文本...因为我还想上传照片。如果我将照片上传到网站上,文本还会在那里吗?? ?

2.我的相机有另一个问题...拍完照片后...它水平显示而不是全屏显示..>您知道我该如何解决吗?

最佳答案

您可以构建一个由图片 + 过度打印的文本组成的新图像,也可以在屏幕上将两者显示为图层。这取决于您是否要保存并重复使用此图像并叠印文本。

据我了解你的问题,这里不是这种情况(如果我错了请告诉我,我会更新这个答案)。所以 FrameLayout 是您的 friend :它允许将图形组件显示为叠印的框架:

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView ...your picture, with the desired attributes />
<TextView ...your text, with the desired attributes />

</FrameLayout>

你可以用 LinearLayout 包围 ImageViewTextView ,使它们带有其他组件,如按钮等。简单地说,您必须将直接包含在 FrameLayout 中的每个组件视为一个新框架,您可以将其设计为任何常规布局。

除了您对本页 xevincent 的回答的评论之外:如果您想显示相机的预览而不是 ImageView,您可以定义自己的 SurfaceView 将负责 Camera 的生命周期:

package com.example;

// imports

public class CameraView extends SurfaceView implements SurfaceHolder.Callback {

public CameraView(Context context, AttributeSet attrs) {
super(context, attrs);
initializeSurfaceHolder();
}

public void surfaceCreated(SurfaceHolder holder) {
// call Camera.open(); and Camera.setPreviewDisplay(holder); here
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Call Camera.setParameters() and Camera.startPreview() here
}

public void surfaceDestroyed(SurfaceHolder holder) {
// Call Camera.stopPreview() and Camera.release() here
}

private void initializeSurfaceHolder() {
// This initializes the SUrfaceHolder of your Camera
final SurfaceHolder holder = getHolder();

holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}

然后,在您的布局中使用此类:

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.example.CameraView
android:id="@+id/previewcam"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<TextView ...your text, also with the desired attributes />

</FrameLayout>

关于android - 在 ImageView 上设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6789283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com