首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓-Extending相对布局/图像过渡

安卓-Extending相对布局/图像过渡
EN

Stack Overflow用户
提问于 2011-06-30 23:35:08
回答 2查看 2.4K关注 0票数 1

我想做一个从一个图像到另一个图像的转换,我正在尝试遵循this示例:

但我仍在学习Android,并且在研究如何让它工作时遇到了问题

我有一个简单的android项目,包含两个Java文件: test和TransitionView,以及一个名为test的布局文件。

测试是我的启动活动,它包含:

代码语言:javascript
复制
    public class test extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
    }
}

TransitionView包含示例中的代码(将图像替换为我自己的一些)

代码语言:javascript
复制
  public class TransitionView extends RelativeLayout {

    /** One of the two in-memory art images */
    private ImageView _artView1;
    /** The other of the two in-memory art images */
    private ImageView _artView2;
    /** Length of art view transition animation, in milliseconds */
    private final int ANIMATION_DURATION_MSEC = 1000;
    /** The underlying ImageSwitcher that performs transitions */
    private ImageSwitcher _imageSwitcher;
    /** Index into _imageIds array */
    private int _currentImage = 0;
    /** All available art image resource ids */

    private final Integer[] _imageIds = { R.drawable.grass,
            R.drawable.headland, R.drawable.horse, R.drawable.icebergs,
            R.drawable.island, R.drawable.sheep, R.drawable.road,
            R.drawable.shrek, R.drawable.waterfall, R.drawable.train };
        /**
     * Create a new instance.
     * 
     * @param context
     *            The parent context
     */
    public TransitionView(Context context) {
        super(context);

        _imageSwitcher = new ImageSwitcher(context);
        Animation fadeIn = AnimationUtils.loadAnimation(context,
                android.R.anim.fade_in);
        fadeIn.setDuration(ANIMATION_DURATION_MSEC);
        Animation fadeOut = AnimationUtils.loadAnimation(context,
                android.R.anim.fade_out);
        fadeOut.setDuration(ANIMATION_DURATION_MSEC);
        _imageSwitcher.setInAnimation(fadeIn);
        _imageSwitcher.setOutAnimation(fadeOut);

        _artView1 = new ImageView(context);
        _artView1.setImageResource(_imageIds[_currentImage]);

        _artView2 = new ImageView(context);
        _artView2.setImageResource(_imageIds[_currentImage + 1]);

        LayoutParams fullScreenLayout = new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        _imageSwitcher.addView(_artView1, 0, fullScreenLayout);
        _imageSwitcher.addView(_artView2, 1, fullScreenLayout);
        _imageSwitcher.setDisplayedChild(0);
        addView(_imageSwitcher, fullScreenLayout);
    }
    /**
     * Change the currently displayed image
     * 
     * @param pageRight
     *            if true, the next image will be shown, else the previous image
     *            will appear
     */
    public void changePage(boolean pageRight) {
        _currentImage = (pageRight) ? (_currentImage + 1) : (_currentImage - 1);
        if (_currentImage < 0) {
            _currentImage = _imageIds.length - 1;
        } else if (_currentImage >= _imageIds.length) {
            _currentImage = 0;
        }

        if (_imageSwitcher.getCurrentView() == _artView1) {
            _artView2.setImageResource(_imageIds[_currentImage]);
            _imageSwitcher.showNext();
        } else {
            _artView1.setImageResource(_imageIds[_currentImage]);
            _imageSwitcher.showPrevious();
        }
    }
}

我的清单如下所示(以防是问题所在):

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
代码语言:javascript
复制
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".test"  android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
          <activity android:name=".TransitionView"></activity>
    </activity>

</application>

我的布局文件,我认为这可能是问题所在:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<TransitionView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"></TransitionView>

当我尝试运行它时,我得到了以下错误:

代码语言:javascript
复制
Unable to start activity ComponentInfo{com.companynamehere.test.imagetransition/Com.companynamehere.test.ImageTransition.test}:android.view.InflateException: Binary Xml file line #2: Error Inflating class Transition View

我知道这与布局和尝试在xml中添加转换视图有关,但我不知道我应该做什么。我尝试将transitionview视图活动设置为start活动,但也不起作用。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-01 16:34:25

它抛出错误的原因是因为它需要一个具有参数"AttributeSet“和上下文的构造函数

而不是这个

代码语言:javascript
复制
  public TransitionView(Context context) {
~~~~~
}

代码语言:javascript
复制
public TransitionView(Context context, AttributeSet attr) {

}

当我问这个问题时,Eclipse没有在logcat中显示这一点,但现在经过一夜的睡眠和PC重启,它是..!

票数 1
EN

Stack Overflow用户

发布于 2011-06-30 23:56:57

您需要在布局XML中使用布局类的完整包名称。如果TransitionView的包名是com.mydomain.myapp,那么您需要在XML中将其引用为:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<com.mydomain.myapp.TransitionView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"></com.mydomain.myapp.TransitionView>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6537320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档