Android - 扩展相对布局/图像转换

标签 android

我想将一张图片转换为另一张图片,我正在尝试遵循 this示例:

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

我有一个带有 2 个 Java 文件的简单 android 项目; test 和 TransitionView 以及一个名为 test 的布局文件。

测试是我的启动 Activity ,包含:

    public class test extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

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

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

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

我的 list 如下所示(如果是问题所在):

<?xml version="1.0" encoding="utf-8"?>

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

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

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

当我尝试运行它时出现以下错误:

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 中添加过渡 View ,但我不知道我应该做什么。 我尝试将 transitionview Activity 设置为开始 Activity ,但这也没有用。

谢谢

最佳答案

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

而不是这个

  public TransitionView(Context context) {
~~~~~
}

这个

public TransitionView(Context context, AttributeSet attr) {

}

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

关于Android - 扩展相对布局/图像转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6537320/

相关文章:

docker 上的 Android SDK

android - 获取当前通知android

android - 我如何从arduino接收蓝牙数据到android?

java - 当我在 Android Studio 上运行我的应用程序时,模拟器启动,但它说测试应用程序崩溃了

Android NDK OpenGL ES 2.0 纹理间距

android - 如何使用 appium 将屏幕截图与引用图像进行比较

Android Studio : Pair devices over Wi-Fi - This system does not meet requirements to support Wi-Fi pairing

android - 如何在 Android OpenGL ES 中应用拖放和缩放

java - 排行榜配置错误

java - Android:从其他类调用 main 中按钮的 OnClick