我的新应用程序的动画和布局问题?

时间:2012-01-17 07:10:57

标签: android android-layout android-emulator android-animation

我正在尝试创建一个新的布局页面,我想放两个按钮,在每个按钮的上方我需要给出一个帧动画。所以在加载时按钮看起来像是在气泡里面。以下是我用来实现此目的的代码:

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_full">
    <Button android:id="@+id/btnMusic"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:gravity="center"
            android:layout_marginLeft="215dp"
            android:layout_marginTop="140dp"
            android:background="@drawable/icon"/>
    <ImageView android:id="@+id/imgMusic"
        android:layout_width="150dp"
            android:layout_height="150dp"
            android:gravity="center"
            android:layout_marginLeft="170dp"
            android:layout_marginTop="100dp"
            android:background="@drawable/button_background"/>      
    <Button android:id="@+id/btnMovies"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:gravity="center"
            android:layout_marginLeft="405dp"
            android:layout_marginTop="140dp"
            android:background="@drawable/icon1"/>
    <ImageView android:id="@+id/imgMovies"
        android:layout_width="150dp"
            android:layout_height="150dp"
            android:gravity="center"
            android:layout_marginLeft="360dp"
            android:layout_marginTop="100dp"
            android:background="@drawable/button_background"/>
    </RelativeLayout>

我的jav代码是这样的:

    public class BubbleActivity extends Activity {
    /** Called when the activity is first created. */
    /** Called when the activity is first created. */
    Button btnMusic, btnMovies ;
    ImageView imgMusic,imgMovies;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
}
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
      super.onWindowFocusChanged(hasFocus);
      btnMusic = (Button)findViewById(R.id.btnMusic);
      btnMovies = (Button)findViewById(R.id.btnMovies);
      btnMusic.setOnClickListener(new View.OnClickListener() 
            {
               public void onClick(View v) {
                  Intent intent = new Intent(PixieActivity.this,Splash.class);
                  startActivity(intent);
               }
            });
      ImageView imgMusic = (ImageView)findViewById(R.id.imgMusic);                                        
      imgMusic.setBackgroundResource(R.drawable.frame_animation);
      AnimationDrawable frameAnimation =(AnimationDrawable) imgMusic.getBackground();  
      if (frameAnimation.isRunning()) {
             frameAnimation.stop();
             }
          else {
         frameAnimation.start();
          }
     ImageView imgMovies = (ImageView)findViewById(R.id.imgMovies);                                        
      imgMovies.setBackgroundResource(R.drawable.frame_animation);
      AnimationDrawable frameAnimation1 =(AnimationDrawable) imgMovies.getBackground();  
      if (frameAnimation1.isRunning()) {
             frameAnimation1.stop();
             }
          else {
             frameAnimation1.start();
          }
    }}

但是由于边距,按钮布局在不同的手机分辨率中分散了注意力。有没有其他方法可以实现相同的布局与设备分辨率无关。另外,我想将气泡动画添加到我将在下一页中制作的每个图标中。请帮忙。

1 个答案:

答案 0 :(得分:0)

我建议不要对边距进行硬编码,而是将Buttons和ImageViews分别包装在LinearLayout中,然后使用layout_weight设置间距,以便完全可扩展。

实际布局选择取决于您是否需要按钮精确到80x80并且ImageView正好是150x150。

例如(伪代码:显然遗漏了许多参数):

<RelativeLayout>
   <LinearLayout id="buttons" >
      <LinearLayout layout_width = "0dp" layout_weight = "1"> <!-- 0 width is important! -->
          <Button layout_gravity="center" />
      </LinearLayout>
      <LinearLayout layout_width = "0dp" layout_weight = "1">
          <Button layout_gravity="center" />
      </LinearLayout>
   </LinearLayout>

   <LinearLayout id="images" align with @buttons>
      <LinearLayout layout_width = "0dp" layout_weight = "1">
          <ImageView layout_gravity="center" />
      </LinearLayout>
      <LinearLayout layout_width = "0dp" layout_weight = "1">
          <ImageView layout_gravity="center" />
      </LinearLayout>
   </LinearLayout>
</RelativeLayout>

并确保将LinearLayouts“按钮”和“图像”设置为相同的高度和全宽,以便按钮和图像重叠。有关layout_weight的更多信息:What does android:layout_weight mean?

如果您不需要将Buttons和ImageView设置为精确尺寸,请考虑按重量调整它们的大小。然后无论你在哪个屏幕上,如果你通过layout_weight告诉按钮占据1/4,它将永远不会被扭曲