2011-07-22 65 views
2

我有一個android應用程序,我用照相手機拍照...並將圖片放在ImageView下。我想要做的是在這張圖片的底部設置文本。在ImageView上設置文本

到現在爲止我做一樣的東西:

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

u能請幫助我,告訴我我該怎麼辦未來的某些代碼將是很好的?!

編輯: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> 

UPDATE:這是我用相機:

....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.I'm不知道這招用的FrameLayout把文本在我的圖片...因爲更多我想上傳照片。如果我在網站上傳照片,文字還會在那裏嗎?

2.我有相機的另一個問題......拍攝照片後......它顯示的是水平而不是全屏......>你知道我該如何解決這個問題嗎?

+0

我有一個XML.I'm沒有得到任何空例外! – adrian

+0

我用XML編輯了我的問題。那麼接下來呢? – adrian

+0

@george我已經編輯我的答案要考慮到您的願望直接看到相機的預覽(而不是ImageView的) – Shlublu

回答

3

您可以構建一個由圖片+圖片疊加而成的新圖片,也可以同時在屏幕上顯示爲圖層。這取決於您是否希望保存並重新使用帶有疊印文本的圖像。

正如我理解你的問題,這裏不是這樣(請讓我知道如果我錯了,我會更新這個答案)。所以FrameLayout是你的朋友:它允許顯示作爲疊印幀的圖形組件:

<?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> 

可以環繞ImageViewTextView通過LinearLayout s到讓他們來與其他組件,如按鈕等簡單地說,你有將直接包含在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> 
當然
+0

請問ü請看看我的更新!? – adrian

+0

是的,我想保存這個圖像上的文字! – adrian

1

您可以使用FrameLayout,把你的形象,並相應

0

在你的文本佈局,創建的ImageView和TextView的方向與垂直和設置文本中的TextView!

0

這個link是關於佈局技巧,但這個例子顯示了你到底想要什麼。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    android:scaleType="center" 
    android:src="@drawable/golden_gate" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="20dip" 
    android:layout_gravity="center_horizontal|bottom" 

    android:padding="12dip" 

    android:background="#AA000000" 
    android:textColor="#ffffffff" 

    android:text="Golden Gate" /> 

</FrameLayout> 
+0

沒錯,但問題是,我的ImageView來自相機是不是繪製;) – adrian