1

選択した画像にユーザー入力を追加しようとしています。これまでのところ、画像を選択でき、エミュレータのキーパッドが表示されますが、画像にテキストが追加されません。以下はコードです。誰でも私がそれを理解するのを手伝ってもらえますか? ありがとう!

public class FullImageActivity extends Activity implements OnClickListener {
private EditText textArea;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    // get intent data
    Intent i = getIntent();

    // Selected image id

    int position = i.getExtras().getInt("id");      
    ImageAdapter imageAdapter = new ImageAdapter(this);     
    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);        
    imageView.setImageResource(imageAdapter.mThumbIds[position]);
    textArea = (EditText)findViewById(R.id.ascii_text);


}

@Override
public void onClick(View arg0) {
    textArea.setText("");
    textArea.getText();
} }

full_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >  

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

<EditText
    android:id="@+id/ascii_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:gravity="center|left"
    android:hint=""
    android:inputType="textMultiLine"
    android:lines="10"
    android:maxLines="10"
    android:minLines="10"
    android:singleLine="false"
    android:textColor="#ffffff"
    android:typeface="monospace" /> 

</LinearLayout>
4

1 に答える 1

0

この方法を試してください

private BitmapDrawable writeTextOnDrawable(int drawableId, String text) {

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)
            .copy(Bitmap.Config.ARGB_8888, true);

    Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);

    Paint paint = new Paint();
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTypeface(tf);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(convertToPixels(mContext, 11));

    Rect textRect = new Rect();
    paint.getTextBounds(text, 0, text.length(), textRect);

    Canvas canvas = new Canvas(bm);

    //If the text is bigger than the canvas , reduce the font size
    if(textRect.width() >= (canvas.getWidth() - 4))     //the padding on either sides is considered as 4, so as to appropriately fit in the text
        paint.setTextSize(convertToPixels(mContext, 7));        //Scaling needs to be used for different dpi's

    //Calculate the positions
    int xPos = (canvas.getWidth() / 2) - 2;     //-2 is for regulating the x position offset

    //"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;  

    canvas.drawText(text, xPos, yPos, paint);

    return new BitmapDrawable(getResources(), bm);
}



public static int convertToPixels(Context context, int nDP)
{
    final float conversionScale = context.getResources().getDisplayMetrics().density;

    return (int) ((nDP * conversionScale) + 0.5f) ;

}

@Arun Georgeの提案

これがあなたが必要とするものであることを願っています

于 2012-12-08T17:22:50.480 に答える