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 回答