#java #android #button #android-custom-view
#java #Android #кнопка #android-пользовательский вид
Вопрос:
Используя кнопку в 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. Найдите представление по id (
gview
) и используйте метод для экземпляра.
Ответ №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>()
...