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

发布于 2024-12-27 12:39:43 字数 3560 浏览 1 评论 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();
          }
    }}

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

I am trying to make a new layout page where I want to put two buttons, and on the above of each button I need to give a frame animation. so on loading the buttons are looking like inside the bubbles. Following is the code I am using to achieve this:

   <?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>

My jav code is like this:

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

But due to the margins the button layout became distracted in different phone resolutions. Is there any other way to achieve the same layout with device resolution independant. Also I want to add the bubble animation to each of the icons i will make in next pages. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2025-01-03 12:39:43

我建议不要对边距进行硬编码,而是将 Button 和 ImageView 分别包装在 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的更多信息:android:layout_weight是什么意思?

如果您不需要按钮如果要使 ImageView 具有精确的大小,请考虑按重量调整它们的大小。那么无论你在什么屏幕上,如果你通过layout_weight告诉一个按钮占据它的1/4,它就永远不会扭曲

I would suggest not hard-coding the margins, and instead wrap the Buttons and ImageViews in a LinearLayout each, then set the spacing using layout_weight so it is perfectly scalable.

The actual layout choice depends on if you need the button to be exactly 80x80 and the ImageView to be exactly 150x150.

For instance (pseudo-code: obviously many parameters are left out) :

<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>

And just make sure set the LinearLayouts "buttons" and "images" to the same height and full width so that the buttons and images overlap. More about layout_weight: What does android:layout_weight mean?

If you do not need the Buttons and ImageViews to be an exact size, think about sizing them by weight as well. Then no matter what screen you are on, if you tell a button to take up 1/4 of it through layout_weight, it will never be distorted

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文