如何从活动中调用自定义视图中的函数(how to call a function that's in a custom View from an activity)

编程入门 行业动态 更新时间:2024-10-14 14:19:27
如何从活动中调用自定义视图中的函数(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>() ...

更多推荐

本文发布于:2023-07-07 21:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   视图   函数   活动中   call

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!