1

この背後にあるロジックに苦労しているので、ヒントをいただければ幸いです。フラグメントが作成されるクラスとカスタム ビューが作成されるクラスの 2 つのクラスがあり、アニメーションを実行しようとしています。メイン レイアウトには、編集テキスト フィールドとボタンがあります。私がやりたいことは、ボタンがクリックされたときにカスタム ビューに追加することです。編集テキスト内のテキストがカスタム ビューに追加されます。これをどうするかを考えていると、私は空白を描いていて、それは不可能だと思い始めています。カスタム ビュー内で編集テキストを作成する必要がありますか? これが私が何をしているのかを示すコードです(しかし、次のステップで立ち往生しているか、このアプローチを破棄して別のアプローチを試す必要があるかどうか)

メインフラグメント

public class DestroyerView extends Fragment
{



    private Context mContext;
    Paint paint = new Paint();
    private AnimatedNegative PositiveAnimatedNegative;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        mContext = this.getActivity();
        View view = inflater.inflate(R.layout.activity_destroyer, container, false);
        final Button fire = (Button) view.findViewById(R.id.destroy);
        PositiveAnimatedNegative = (AnimatedNegative) view.findViewById(R.id.anim_view);
        fire.setOnClickListener(new OnClickListener()
            {
            @Override
            public void onClick(View arg0) 
                {
                // logic here?
                }
            });
       return view;

    }
}


public AnimatedNegative(Context context, AttributeSet attrs)  

{

        super(context, attrs);


        mContext = this.getContext();

        h = new Handler();
        mCalendarDbHelper=new CalendarDbAdapter(mContext);
        mCalendarDbHelper.open();


}    

    private Runnable r= new Runnable() 
    {
    @Override
    public void run() 
            {
        invalidate(); 
    }
    };



    protected void onDraw (Canvas canvas)
    {
     String word = "This is a sentence";
     paint.setColor(Color.parseColor("#1E90FF")); 
     paint.setStyle(Style.FILL); 
     canvas.drawPaint(paint); 
     paint.setColor(Color.BLACK); 
     paint.setTextSize(20); 
     x = this.getWidth()/2;
     y = this.getHeight()/2;
     canvas.drawText(word, x, y, paint);
     }
   }

...カスタムビュー内のボタンを参照できれば、これははるかに簡単になりますが、それができないようです。または、メインアクティビティクラス内のカスタムビューに単純に追加できれば、私にもそれができるようには見えません(新しいキャンバスを作成しないと、私がやりたいことに対してやりすぎているようです(言葉を追加するだけです))。それで、最後に、私が現在これをやろうとしている方法は行き止まりですか?どんな助けでも大歓迎です。

4

1 に答える 1

2

私はそれを手に入れたと思います。メイン アクティビティで定義された AnimatedNegative オブジェクトを使用して invalidate を呼び出し、編集テキストの単語と true 値を渡して、ビューで確認して再描画できるようにします。以下に示すコードを追加しました。

主なアクティビティ内:

        public void onClick(View arg0) 
            {
            PositiveAnimatedNegative.invalidate();
            PositiveAnimatedNegative.add = true;
            PositiveAnimatedNegative.positive_word = "This is a positive word";
            }

そして、カスタム ビュー内:

            if (add == true)
            {
                canvas.drawText(positive_word, x,y, paint);
            }
于 2013-02-14T18:31:12.000 に答える