Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式根据屏幕方向设置int值_Android - Fatal编程技术网

Android 以编程方式根据屏幕方向设置int值

Android 以编程方式根据屏幕方向设置int值,android,Android,我想根据屏幕方向为int设置不同的值..我已经尝试过..但是从if bloc开始很多未解析的代码都不起作用 PS:1/Mindicator高度是指示器在视图底部占据的空间高度。用于对recyclerview进行分页。。 2/为回收者分页的指示器完全是用代码编写的,因此我不能使用不同的xml文件 LinePageIndicatorDecoration.java public class LinePagerIndicatorDecoration extends RecyclerView.Item

我想根据屏幕方向为int设置不同的值..我已经尝试过..但是从if bloc开始很多未解析的代码都不起作用

PS:1/Mindicator高度是指示器在视图底部占据的空间高度。用于对recyclerview进行分页。。 2/为回收者分页的指示器完全是用代码编写的,因此我不能使用不同的xml文件

LinePageIndicatorDecoration.java

  public class LinePagerIndicatorDecoration extends RecyclerView.ItemDecoration {
        RecyclerViewPositionHelper mRecyclerViewHelper;
        int firstVisibleItem;
        private int colorActive = 0xFFFFFFFF;
private int mIndicatorHeight;
        private int colorInactive = 0x66FFFFFF;
        private Context context;

        private static final float DP = Resources.getSystem().getDisplayMetrics().density;

        /**
         * Height of the space the indicator takes up at the bottom of the view.
         */
        int orientation = this.context.getResources().getConfiguration().orientation;

        if (orientation == Configuration.ORIENTATION_PORTRAIT)
        {
             mIndicatorHeight = (int) (DP * 200);
        }
        else if( orientation == Configuration.ORIENTATION_LANDSCAPE) {

             mIndicatorHeight = (int) (DP * 100);
        }

        /**
         * Indicator stroke width.
         */

        private final float mIndicatorStrokeWidth = DP * 2;

        /**
         * Indicator width.
         */
        private final float mIndicatorItemLength = DP * 16;
        /**
         * Padding between indicators.
         */
        private final float mIndicatorItemPadding = DP * 4;

        /**
         * Some more natural animation interpolation
         */
        private final Interpolator mInterpolator = new AccelerateDecelerateInterpolator();

        private final Paint mPaint = new Paint();

        public LinePagerIndicatorDecoration() {
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(mIndicatorStrokeWidth);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setAntiAlias(true);
        }

        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDrawOver(c, parent, state);

            int itemCount = parent.getAdapter().getItemCount();

            // center horizontally, calculate width and subtract half from center
            float totalLength = mIndicatorItemLength * itemCount;
            float paddingBetweenItems = Math.max(0, itemCount - 1) * mIndicatorItemPadding;
            float indicatorTotalWidth = totalLength + paddingBetweenItems;
            float indicatorStartX = (parent.getWidth() - indicatorTotalWidth) / 2F;

            // center vertically in the allotted space
            float indicatorPosY = parent.getHeight() - mIndicatorHeight / 2F;

            drawInactiveIndicators(c, indicatorStartX, indicatorPosY, itemCount);

            mRecyclerViewHelper = RecyclerViewPositionHelper.createHelper(parent);
            //firstVisibleItem = mRecyclerViewHelper.findFirstVisibleItemPosition();

            // find active page (which should be highlighted)
            RecyclerView.LayoutManager layoutManager = (RecyclerView.LayoutManager) parent.getLayoutManager();
            int activePosition = mRecyclerViewHelper.findFirstCompletelyVisibleItemPosition();
            if (activePosition == RecyclerView.NO_POSITION) {
                return;
            }

            // find offset of active page (if the user is scrolling)
            final View activeChild = layoutManager.findViewByPosition(activePosition);
            int left = activeChild.getLeft();
            int width = activeChild.getWidth();

            // on swipe the active item will be positioned from [-width, 0]
            // interpolate offset for smooth animation
            float progress = mInterpolator.getInterpolation(left * -1 / (float) width);

            drawHighlights(c, indicatorStartX, indicatorPosY, activePosition, progress, itemCount);
        }

        private void drawInactiveIndicators(Canvas c, float indicatorStartX, float indicatorPosY, int itemCount) {
            mPaint.setColor(colorInactive);

            // width of item indicator including padding
            final float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;

            float start = indicatorStartX;
            for (int i = 0; i < itemCount; i++) {
                // draw the line for every item
                c.drawLine(start, indicatorPosY, start + mIndicatorItemLength, indicatorPosY, mPaint);
                start += itemWidth;
            }
        }

        private void drawHighlights(Canvas c, float indicatorStartX, float indicatorPosY,
                                    int highlightPosition, float progress, int itemCount) {
            mPaint.setColor(colorActive);

            // width of item indicator including padding
            final float itemWidth = mIndicatorItemLength + mIndicatorItemPadding;

            if (progress == 0F) {
                // no swipe, draw a normal indicator
                float highlightStart = indicatorStartX + itemWidth * highlightPosition;
                c.drawLine(highlightStart, indicatorPosY,
                        highlightStart + mIndicatorItemLength, indicatorPosY, mPaint);
            } else {
                float highlightStart = indicatorStartX + itemWidth * highlightPosition;
                // calculate partial highlight
                float partialLength = mIndicatorItemLength * progress;

                // draw the cut off highlight
                c.drawLine(highlightStart + partialLength, indicatorPosY,
                        highlightStart + mIndicatorItemLength, indicatorPosY, mPaint);

                // draw the highlight overlapping to the next item as well
                if (highlightPosition < itemCount - 1) {
                    highlightStart += itemWidth;
                    c.drawLine(highlightStart, indicatorPosY,
                            highlightStart + partialLength, indicatorPosY, mPaint);
                }
            }
        }
怎样做才合适谢谢

编辑:BenP后出现错误异常。解决方案

java.lang.RuntimeException: Unable to start activity ComponentInfo{ma.ac.iav.menunaviagtion/ma.ac.iav.menunavigation.testanat.JustCoverFlowActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at com.github.bleeding182.recyclerviewdecorations.viewpager.LinePagerIndicatorDecoration.<init>(LinePagerIndicatorDecoration.java:55)
    at ma.ac.iav.menunavigation.testanat.JustCoverFlowActivity.onCreate(JustCoverFlowActivity.java:57)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)

假设没有任何理由要求您必须以编程方式完成此任务,那么最好的解决方案是利用Android资源框架为您完成此任务

第一步是创建两个不同的dimens.xml文件,一个用于纵向,另一个用于横向。您的目录结构应如下所示:

src/
    main/
        res/
            values/
                dimens.xml
            values-land/
                dimens.xml
dimens.xml将定义维度资源。肖像画:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="indicator_height">200dp</dimen>
</resources>
由于此尺寸标注既有值/也有值/版本,因此您会发现高度值会根据设备方向发生变化


请注意,高度将以像素为单位,因此不可能仅为200或100。许多屏幕的每dp大约有2.5个像素,所以你可以看到500或250个像素,尽管你的设备可以是任何东西,真的。

很多未解析的代码,从bloc开始,如果bloc是特定的,并发布完整的代码,类等等。你为什么要以编程方式执行此操作?我的一般建议是使用资源框架来解决这个问题。@Pavneet_Singh,代码的其余部分与此无关。。但我应该声明,此代码属于非活动类。..PS该类用于分页recyclerview@BenP. 如何做到这一点..我现在已经进入android dvlpt 2个月了..所以我是新手,谢谢你的帮助help@Pavneet_Singh对于未解析的代码..我的意思是从if区到我发布的代码的结尾..不是整个类..thansk的帮助非常感谢您的回复..但是如何为每个屏幕类型xxhdpi创建土地价值,xxhdpi..因为我必须申请每种屏幕类型..没关系..我在互联网上找到了如何做..我会尝试告诉你结果,我得到了一个异常错误。我在帖子中添加了它..你能看到请解决它..谢谢你的回答
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="indicator_height">100dp</dimen>
</resources>
int height = context.getResources().getDimensionPixelSize(R.dimen.indicator_height);