应用在使用TransitionDrawable时崩溃。

huangapple go评论72阅读模式
英文:

App crashes when using TransitionDrawable

问题

以下是翻译的内容:

我想在按钮被点击时使背景颜色过渡变化,但是一旦我按下按钮,我的应用似乎会崩溃。按钮运行TransitionDrawable代码。我已将背景设置为包含transition的drawable文件夹。有人可以帮助我吗?非常感谢!

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void colourChangeButton(View view) {
        final TransitionDrawable transition = (TransitionDrawable) view.getBackground();
        transition.startTransition(1000);
    }
}

这是定义颜色的我的可绘制文件:

第一个文件(blue_background.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="#2575fc"></color>
    </item>
</selector>

第二个文件(pink_background.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <color android:color="#ff0844"></color>
    </item>
</selector>

这是我的过渡可绘制文件(transition.xml):

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/blue_background"></item>
    <item android:drawable="@drawable/pink_background"></item>
</transition>
英文:

I want to transition the color of my background when a button is clicked but my app seems to crash once I press the button. The button runs the TransitionDrawable code. I have set my background to the drawable folder containing the transition.Could anyone help me? Thank you very much!

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void colourChangeButton(View view) {

       final TransitionDrawable transition = (TransitionDrawable) view.getBackground();
        transition.startTransition(1000);

  }
}

Here are my drawable files that define the colours:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;selector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;

    &lt;item&gt;

        &lt;color android:color=&quot;#2575fc&quot;&gt;&lt;/color&gt;
    &lt;/item&gt;

&lt;/selector&gt;




&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;selector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;

    &lt;item&gt;

        &lt;color android:color=&quot;#ff0844&quot;&gt;&lt;/color&gt;
    &lt;/item&gt;

&lt;/selector&gt;

Here is my transition drawable file :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;transition xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;

&lt;item android:drawable=&quot;@drawable/blue_background&quot;&gt;&lt;/item&gt;
    &lt;item android:drawable=&quot;@drawable/pink_background&quot;&gt;&lt;/item&gt;
    &lt;/transition&gt;

答案1

得分: 1

你离成功不远了!

你需要将过渡可绘制对象设置为视图的背景。

假设你的活动布局 XML 名称为 activity_main.xml,你的过渡可绘制文件名为 transition.xml

在你的 activity_main.xml 的根布局中:

<LinearLayout
   ... 
   android:id="@+id/main_layout"
   android:background="@drawable/transition">

    <Button
        android:id="@+id/start_transition_button"
        ... />
    ...

MainActivity.java 中:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_main);
    
        ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
        TransitionDrawable transition = (TransitionDrawable) layout.getBackground();
        Button button = (Button) findViewById(R.id.start_transition_button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                transition.startTransition(500);
            }
        });
    }
}

以下是一篇关于此的优质文章:
https://proandroiddev.com/transitiondrawable-small-gems-of-the-android-framework-4dcdd3c83319

英文:

You are almost there!

You need to set the transition drawable as the background of your view.

Let's say your activity's layout xml is called activity_main.xml and your
transition drawable file is called transition.xml.

In the root layout of your activity_main.xml:

&lt;LinearLayout
   ... 
   id=&quot;@+id/main_layout&quot;
   background=&quot;@drawable/transition&quot;&gt;

    &lt;Button
        id=&quot;@+id/start_transition_button&quot;
        ... /&gt;
    ...

In MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_main);
    
        ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
        TransitionDrawable transition = (TransitionDrawable) layout.getBackground();
        Button button = (Button) findViewById(R.id.start_transition_button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                transition.startTransition(500);
            }
        }
    }
}

Here is a good post about it:
https://proandroiddev.com/transitiondrawable-small-gems-of-the-android-framework-4dcdd3c83319

huangapple
  • 本文由 发表于 2020年9月16日 23:39:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63923513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定