我试图找出在引用我的表面视图类时出错的地方,而不是我的观点。我只把视图作为例子。
这是我的主要课程:
package com.example;
import android.app.Activity;
import android.os.Bundle;
public class AnimationActivity extends Activity {
/** Called when the activity is first created. */
Drwwingtheball v;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
v = new Drwwingtheball(this);
setContentView(v);
}
}
这是我的观点类:
package com.example;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class Drwwingtheball extends View{
Bitmap bball;
int x, y;
public Drwwingtheball(Context context) {
super(context);
bball = BitmapFactory.decodeResource(getResources(), R.drawable.newbball);
x=0;
y=0;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Rect ourrect = new Rect();
ourrect.set(0, 0, canvas.getWidth(), canvas.getHeight()/2);
Paint blue = new Paint();
Paint green = new Paint();
blue.setColor(Color.BLUE);
blue.setStyle(Paint.Style.FILL);
green.setColor(Color.GREEN);
if (x < canvas.getWidth()){
x+=12;
}else{
x=0;
}
if (y < canvas.getHeight())
{
y +=8;
}else{
y=0;
}
canvas.drawBitmap(bball, x , y, new Paint(green));
invalidate();
}
}
这是我的表面视图Class:
package com.example;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class surfaceviewexample extends Activity implements OnTouchListener{
ourView v;
Bitmap bball;
float x, y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = new ourView(this);
v.setOnTouchListener(this);
bball = BitmapFactory.decodeResource(getResources(), R.drawable.bball);
setContentView(v);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class ourView extends SurfaceView implements Runnable{
Thread t=null;
SurfaceHolder holder;
boolean isitok=false;
public ourView(Context context) {
super(context);
holder = getHolder();
}
public void run(){
while(isitok == true){
if(!holder.getSurface().isValid()){
continue;
}
Canvas c= holder.lockCanvas();
c.drawARGB(255, 150, 155, 25);
c.drawBitmap(bball, x-bball.getWidth()/2, y=bball.getHeight()/2, null);
holder.unlockCanvasAndPost(c);
}
}
public void pause(){
isitok=false;
while(true){
try{
t.join();
}catch( InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume(){
isitok=true;
t = new Thread(this);
t.start();
}
}
@Override
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
x = me.getX();
y = me.getY();
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
x = me.getX();
y = me.getY();
break;
case MotionEvent.ACTION_UP:
x = me.getX();
y = me.getY();
break;
case MotionEvent.ACTION_MOVE:
x = me.getX();
y = me.getY();
break;
}
return true;
}
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AnimationActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
尝试将onCreate
中的AnimationActivity
方法更改为以下内容:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
v = new surfaceviewexample(this);
setContentView(v);
}
理解你想要的东西有点困难。试试看,看看是否能解决你的问题。如果没有,你能尝试更好地解释一下你的问题吗?
由于