0

如何绑定类 TextView 和 BaseAdapter 将参数传递给 TextView ?

我的 BaseAdapter.class

public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.item_grid, null);

            view.txtViewTitle = (TextView) convertView
                    .findViewById(R.id.myImageViewText);

            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }
        view.txtViewTitle.setText(list.get(position));

        return convertView;
    }

我的 xml

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/img_1" />

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingTop="5dip" >

    <vbright.usanin.salesRegion.Text.TextViewOutline
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

和文本视图...

@Override
    protected void onDraw(Canvas canvas) {

        Paint strokePaint = new Paint();
        strokePaint.setARGB(255, 0, 0, 0);
        strokePaint.setTextAlign(Paint.Align.CENTER);

        strokePaint.setTextSize(20);
        strokePaint.setTypeface(Typeface.DEFAULT_BOLD);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setStrokeWidth(5);

        Paint textPaint = new Paint();
        textPaint.setARGB(255, 255, 255, 255);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(20);
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);

        int width = getWidth();

        canvas.drawText("Some Text", (160 - width) / 2, 20, strokePaint);   - **how to transfer parameters here??**
        canvas.drawText("Some Text", (160 - width) / 2, 20, textPaint);

    }
4

2 回答 2

0

您需要使用字符串创建位图吗?

我用这个:

        // String to bitmap 
    public Bitmap StringToBitMap(String encodedString){
            TextView textView = new TextView(StoryActivity.this); 
            textView.layout(30, 300, 300, 500);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 12);
            textView.setTextColor(Color.BLACK);
            textView.setText(encodedString); 
            textView.setDrawingCacheEnabled(true); 
            textView.setDrawingCacheQuality(2);

            return Flip(textView.getDrawingCache()) ;
    }

    // flip picture
    private Bitmap Flip(Bitmap asset) {

            Matrix mat = new Matrix();
            mat.postScale(-1F, 1F); 
            int width = asset.getWidth();
            int height = asset.getHeight();

            return Bitmap.createBitmap(asset, 0, 0, width, height, mat, true);
    }
于 2012-11-05T12:56:35.643 回答
0

在 TextView 中调用 invalidate() 方法将重绘 TextView。对 TextView 中的 String 变量使用 getter 和 setter 方法,并在 onDraw 中使用该 String 来绘制文本。

也不要忘记调用 BaseAdapter.notifyDataSetChanged 方法来刷新您的适配器。

而已。

于 2012-11-05T13:07:05.040 回答