如何从活动中调用自定义视图中的函数

时间:2014-06-22 17:00:18

标签: java android button android-custom-view

使用MainActivity中的按钮如何调用必须在sprite.move("left")中运行的GView(这会将精灵每秒移动两次)?

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);  
        Button button = (Button) findViewById(R.id.button);
        button.setOnTouchListener(new View.OnTouchListener() {

            private Handler mHandler;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (mHandler != null)
                        return true;
                    mHandler = new Handler();
                    mHandler.postDelayed(mAction, 0);
                    break;
                case MotionEvent.ACTION_UP:
                    if (mHandler == null)
                        return true;
                    mHandler.removeCallbacks(mAction);
                    mHandler = null;
                    break;
                }
                return false;
            }

            Runnable mAction = new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "Performing action...", Toast.LENGTH_LONG).show();
                    mHandler.postDelayed(this, 500);
                }
            };

        });
    }
}

main.xml中

  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >

<com.viracide.depth.GView
   android:id="@+id/gview"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_alignParentLeft="true"
   android:layout_alignParentRight="true"
   android:layout_alignParentTop="true"
   android:layout_marginBottom="180dp"
   android:layout_marginLeft="40dp"
   android:layout_marginTop="40dp"
   android:layout_marginRight="40dp">
</com.viracide.depth.GView>

<Button
   android:id="@+id/button"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/gview"
   android:layout_alignParentBottom="true"
   android:layout_marginBottom="73dp"
   android:text="Button" />

</RelativeLayout>

GView.java

public class GView extends View {
    private Bitmap bmp;
    sprite sprite; //sprite image

    public GView(Context context) {
        super(context);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bit);
    }

    public void draw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bmp, 10 , 10, null);
    }
}

1 个答案:

答案 0 :(得分:1)

public class MainActivity extends Activity {
GView mGView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);  
    // initialize here
    mGView = (GView)findViewById(R.id.gview);
    // put this anywhere and make sure you do not violate UI thread constraint for making any UI changes
    mGview.<yourmethod>()
 ...