0

MainActivityのレイアウトにいくつかのボタンSPINNERとEditTextを追加していますが、ビットマップをspinner(MainActivityクラス)からCustomViewに取得する方法をいくつか試しましたが、失敗しました。私を助けてください!どんな助けでも大歓迎です!ありがとう!

TextActivity.java

  package com.example.textdrawn2;
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Paint;
    import android.graphics.Typeface;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.AdapterView.OnItemSelectedListener;

    public class TextActivity extends Activity{

TextView tv;
ImageView iv;
EditText et;
Button b;
public static Bitmap bmap;
Paint paint;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_text);


    //Initialise editText, textView & Button
    et = (EditText) findViewById(R.id.editText1);     
    tv = (TextView) findViewById(R.id.textView1);


    //Spinner (getting texts to be displayed inside drop down box)
    Spinner fontspinner = (Spinner) findViewById(R.id.fonts_spinner);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
         R.array.fonts_array, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    fontspinner.setAdapter(adapter);

    //Initialise Spinner
    Spinner fonts_sp;
    fonts_sp = (Spinner) findViewById(R.id.fonts_spinner);
    fonts_sp.setOnItemSelectedListener(myListener);



}


OnItemSelectedListener myListener = new OnItemSelectedListener() { 



        public void onItemSelected(AdapterView<?> parent, View view, 
                int pos, long id) {

            //Initialise Button, ET and TV
            Button b = (Button) findViewById(R.id.btnText);
            et = (EditText) findViewById(R.id.editText1);
            tv = (TextView) findViewById(R.id.textView1);
            //iv = (ImageView) findViewById(R.id.imageView1);

            //To enable program to switch to a different font
            switch(pos) {

            case 0: // When user select Code 39 font
                    b.setOnClickListener(new OnClickListener() {      
                        @Override     
                        public void onClick(View view) {          
                            if (et.getText().toString().equals("")) {

                                //Clear textView
                                tv.setText("");          
                                et.setText("");
                                tv.buildDrawingCache();
                                iv.setImageBitmap(tv.getDrawingCache());

                            }
                            else { 

                                //To change to Code 39 barCode font
                                Typeface df = Typeface.createFromAsset(getAssets(),                 
                                        "fonts/IDAutomationHC39M_FREE.otf");         
                                TextView tv1 = (TextView) findViewById(R.id.textView1);         
                                tv1.setTypeface(df);

                                String editTextValue = et.getText().toString();            
                                tv.setText(editTextValue);

                                ImageView iv = (ImageView) findViewById(R.id.imageView1);
                                tv.buildDrawingCache();
                                bmap = tv.getDrawingCache();
                                iv.setImageBitmap(bmap);

                            }          
                        }
                    });
                    break;

            case 1: // When user select Code 128 font

                b.setOnClickListener(new OnClickListener() {      
                    @Override     
                    public void onClick(View view) {          
                        if (et.getText().toString().equals("")) {

                            //Clear textView
                            tv.setText("");          
                            et.setText("");
                            tv.buildDrawingCache();
                            iv.setImageBitmap(tv.getDrawingCache());
                        }
                        else { 

                            //To change to Code 128 barCode font
                            Typeface df = Typeface.createFromAsset(getAssets(),                 
                                    "fonts/code128.ttf");         
                            TextView tv1 = (TextView) findViewById(R.id.textView1);         
                            tv1.setTypeface(df);

                            String editTextValue = et.getText().toString();            
                            tv.setText(editTextValue);

                            tv.buildDrawingCache();
                            iv.setImageBitmap(tv.getDrawingCache());
                        }          
                    }
                });
                break;

            case 2: // When user select MOLOT font
                    b.setOnClickListener(new OnClickListener() {      
                        @Override     
                        public void onClick(View view) {          
                            if (et.getText().toString().equals("")) {

                                //Clear textView
                                tv.setText("");          
                                et.setText("");
                                tv.buildDrawingCache();
                                iv.setImageBitmap(tv.getDrawingCache());

                            }
                            else { 

                                //To change to Code 39 barCode font
                                Typeface df = Typeface.createFromAsset(getAssets(),                 
                                        "fonts/Molot.otf");         
                                TextView tv1 = (TextView) findViewById(R.id.textView1);         
                                tv1.setTypeface(df);

                                String editTextValue = et.getText().toString();            
                                tv.setText(editTextValue);

                                tv.buildDrawingCache();
                                iv.setImageBitmap(tv.getDrawingCache());
                            }          
                        }
                    });
                    break;

Text.Java(CustomView)

public class Text extends View implements OnTouchListener {

Typeface fonts ;


public Text(Context context,AttributeSet attrs) {
    super(context, attrs);

    //fonts = Typeface.createFromAsset(context.getAssets(), "Molot.otf");
    //et = (EditText) findViewById(R.id.editText1);

    }


@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.drawColor(Color.BLUE);
    //canvas.drawBitmap(bmap, 0, 100, null);

}

@Override
public boolean onTouch(View v, MotionEvent event) {

    return true;

}

}

4

1 に答える 1

0

設定するクラスには、Bitmapそのためのメソッドが必要です。

Bitmap bitmap;

public void setBitmap(Bitmap bitmap) {
    this.bitmap = bitmap;
}
于 2013-01-14T03:16:32.063 に答える