It uses bitmaps and threads to change the direction of the character whenever it moves and hits the wall I want to put the class in one view in the XML layout, what should I do? I can't understand why even if I look at the error message when I run itcrying Intuitively speaking, I think we can do it like this, but I'd appreciate it if you could tell me which part you're not doing well.
Class to move characters
class CharacterView extends View {
int width, height; // width and height of screen
int x, y; // the character's current coordinates
int dx, dy; // the direction and distance the character will travel
int cw, ch; // width and height of character
int counter; // loop counter
Bitmap character[] = new Bitmap[2]; // character's bitmap image
LinearLayout character_field = (LinearLayout)findViewById(R.id.character_field);
public CharacterView(Context context) {
super(context);
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
width = character_field.getWidth(); // width of screen
height = character_field.getHeight(); // width of screen
x = 100; // the current x position of the character
y = 100; // the current y position of the character
dx = 2; // the distance the character moves to the x-axis
dy = 3; // the distance the character moves to the y-axis
// Read the character's bitmap
character[0] = BitmapFactory.decodeResource(getResources(), R.drawable.tmp1);
character[1] = BitmapFactory.decodeResource(getResources(), R.drawable.tmp2);
cw = character[0].getWidth() / 2; // width of character/2
ch = character[0].getHeight() / 2; // height of character/2
mHandler.sendEmptyMessageDelayed(0, 10);
}
//-----------------------------------
// The part where you draw the picture
//-----------------------------------
public void onDraw(Canvas canvas) {
x + = dx; // Move horizontally
y + = dy; // Move longitudinally
If (x < cw || x > width - cw) dx = -dx; // Walls on the left and right are reversed
If (y < ch || y > height - ch) dy = -dy; // Change direction if ceiling or floor
counter++;
int n = counter % 20 / 10;
canvas.drawBitmap(character[n], x - cw, y - ch, null);
} // End of Draw
//------------------------------------
// // Timer Handler
//------------------------------------
Handler mHandler = newHandler() { // Handler to use as timer
public void handleMessage(Message msg) {
validate(); // onDraw() rerun
mHandler.sendEmptyMessageDelayed(0,10); // Run every 10/1000 seconds
}
}; }; // Handler
} // GameView End
XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/character_field"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="horizontal"
android:gravity="center"
>
<helloworld.sdh.com.termex04.CharacterView
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
Main Activity
public class LockActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lock);
//CharacterView cv = new CharacterView(this);
//LinearLayout character_field = (LinearLayout)findViewById(R.id.character_field);
//character_field.addView(cv);
}
}
The first error screen is an error caused by not overriding a constructor with two parameters in the View class. Override the CharacterView(Context context, AttributeSetattrs)
constructor in the CharacterView code you wrote as follows:
public CharacterView(Context context) {
this(context, null);
}
public CharacterView(Context context, AttributeSet attrs) {
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
width = character_field.getWidth(); // width of screen
height = character_field.getHeight(); // width of screen
x = 100; // the current x position of the character
y = 100; // the current y position of the character
dx = 2; // the distance the character moves to the x-axis
dy = 3; // the distance the character moves to the y-axis
// Read the character's bitmap
character[0] = BitmapFactory.decodeResource(getResources(), R.drawable.tmp1);
character[1] = BitmapFactory.decodeResource(getResources(), R.drawable.tmp2);
cw = character[0].getWidth() / 2; // width of character/2
ch = character[0].getHeight() / 2; // height of character/2
mHandler.sendEmptyMessageDelayed(0, 10);
}
When the constructor of View is called, the first constructor is called when creating View from code, and the second constructor when inflating View over XML. Therefore, when you create View over XML, you must override the second constructor.
For more information about constructors, see the questions and answers in the following link.
© 2024 OneMinuteCode. All rights reserved.