0

我想做一个新的布局页面,我想放两个按钮,并在上面的每个按钮,我需要给帧动画。所以加载按钮看起来像泡泡内。以下是我使用来实现这个代码:动画和布局问题与我的新应用程序?

<?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(); 
      } 
    }} 

但由于按键布局成为不同分辨率的手机分心的利润。有没有其他方法可以实现与设备分辨率无关的相同布局。另外我想将泡泡动画添加到我将在下一页中制作的每个图标中。请帮忙。

回答

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和ImageViews是一个确切的大小,请考虑按重量大小调整它们。那么不管你在哪个屏幕上,如果你告诉按钮通过layout_weight占据它的1/4,它将永远不会被扭曲