2014-07-08 5 views
0

:기존 레이아웃에 animatedView 추가 좋아, 그래서 여기에 내 전체 코드

package com.example.theball; 

import java.util.Timer; 
import java.util.TimerTask; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.content.pm.ActivityInfo; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Point; 
import android.graphics.drawable.ShapeDrawable; 
import android.graphics.drawable.shapes.OvalShape; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.view.Display; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

@SuppressLint("NewApi") 
@SuppressWarnings("unused") 
public class MainActivity extends Activity implements SensorEventListener { 
    private SensorManager sensorManager; 
    private Sensor accelerometer; 
    private long lastUpdate; 
    AnimatedView animatedView = null; 
    ShapeDrawable mDrawable = new ShapeDrawable(); 
    public static int x; 
    public static int y; 
    public static final int width = 50; 
    public static final int height = 50; 
    public boolean firstDraw = true; 
    private int screen_width; 
    private int screen_height; 
    private int sensorX; 
    private Timer t; 
    private int TimeCounter = 0; 
    private TextView highscore_int; 
    private int sensorY; 

    @SuppressWarnings("deprecation") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     highscore_int = (TextView) findViewById(R.id.highscore_int); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
     accelerometer = sensorManager 
       .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     lastUpdate = System.currentTimeMillis(); 
     animatedView = new AnimatedView(this); 
     setContentView(animatedView); 
     t = new Timer(); 
     t.scheduleAtFixedRate(new TimerTask() { 
      public void run() { 
       runOnUiThread(new Runnable() { 
        public void run() { 
         TimeCounter++; 
        } 
       }); 
      } 
     }, 0, 1000); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     sensorManager.registerListener(this, accelerometer, 
       SensorManager.SENSOR_DELAY_GAME); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     sensorManager.unregisterListener(this); 
    } 

    @Override 
    public void onAccuracyChanged(Sensor arg0, int arg1) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     // TODO Auto-generated method stub 
     if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
      sensorY = (int) event.values[1]; 
      sensorX = (int) event.values[0]; 
      x -= sensorX * 3; 
      y += sensorY * 3; 
      if (x <= 0 || x >= screen_width || y <= 0 || y >= screen_height) { 
       finish(); 
       Intent myIntent = new Intent(this, YouLost.class); 
       startActivity(myIntent); 
      } 
     } 
    } 


    public class AnimatedView extends ImageView { 

     Display display = getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     static final int width = 50; 
     static final int height = 50; 

     @SuppressLint("NewApi") 
     public AnimatedView(Context context) { 
      super(context); 
      // TODO Auto-generated constructor stub 
      display.getSize(size); 
      screen_width = size.x; 
      screen_height = size.y; 
      mDrawable = new ShapeDrawable(new OvalShape()); 
      mDrawable.getPaint().setColor(0xffffAC23); 
      mDrawable.setBounds(0, 0, screen_width, screen_height); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      mDrawable.setBounds(x, y, x + width, y + height); 
      if (firstDraw) { 
       x = screen_width/2; 
       y = screen_height/2; 
       firstDraw = false; 
      } 
      mDrawable.draw(canvas); 
      invalidate(); 
     } 
    } 
} 

내 문제는 내가 animatedView가이 여전히 activity_main 레이아웃을 사용하려는이며, 내가 어떻게 할 수 있습니까? 제발, 내 문제가 내가 두번이나 setContentView으로 설정되었다고 말하지 마라. 나는 그걸 알았다. 왜냐하면 내가 공과 레이아웃을 원하기 때문입니다. 내 문제와 다른

다음

사람 : http://stackoverflow.com/questions/22580336/android-animatedview-taking-up-whole-activity-screen .

나는 "setContentView(animatedView)" 내가 내 레이아웃하지만 내 공을해야합니다 삭제됩니다 경우, 자신을 위해 빈 레이아웃을 만들기 animatedView.

이제 공이있는 빈 레이아웃이 있습니다.

답변

0

activity_main에 대한 XML을 게시 할 수 있습니까?

활동 레이아웃의 최상위보기로 FrameLayout을 만드는 것이 좋습니다. 이렇게하면 자식 뷰를 다른 뷰 위에 쌓을 수 있습니다. 당신이 animatedView의 z 순서를 지정하려면

FrameLayout myRootLayout = (FrameLayout) findViewById(R.id.id_of_top_level_framelayout); 
animatedView = new AnimatedView(this); 
myRootLayout.addView(animatedView); 

는 다른 방법으로 당신이 addView(animatedView, index)를 사용할 수 있습니다 다음, setContentView(R.layout.activity_main)에 전화 후, 당신은 이런 식으로 뭔가를 할 수 있습니다.

질문에 잘못 이해했는지 확인하십시오.