如何从活动中调用自定义视图中的函数(how to call a function that's in a custom View from an activity)
使用MainActivity的按钮如何调用sprite.move("left") (它将精灵每秒移动两次)?
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); } }Using the button in MainActivity how can i call sprite.move("left") (which will move the sprite left twice a second) that has to be run in 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); } }最满意答案
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>() ... 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>() ...
更多推荐
发布评论