Android Open Source - savemypics C Maximum Width Layout






From Project

Back to project page savemypics.

License

The source code is released under:

Copyright (c) 2014, KB Sriram All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. R...

If you think the Android project savemypics listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.savemypics.android.view;
//from  w w  w.j a va  2s .co m
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import org.savemypics.android.R;
import org.savemypics.android.util.CUtils;

public class CMaximumWidthLayout extends ViewGroup
{
    public CMaximumWidthLayout(Context ctx, AttributeSet attrs)
    {
        super(ctx, attrs);

        TypedArray a = ctx.obtainStyledAttributes
            (attrs, R.styleable.CMaximumWidthLayout);
        try {
            m_max_child_width = a.getDimensionPixelSize
                (R.styleable.CMaximumWidthLayout_maximumChildWidth, 0);
            m_child_gravity = a.getInt
                (R.styleable.CMaximumWidthLayout_childGravity, CG_CENTER);
        }
        finally {
            a.recycle();
        }
    }

    @Override
    protected void onMeasure(int wspec, int hspec)
    {
        int wmode = MeasureSpec.getMode(wspec);
        int wsize = MeasureSpec.getSize(wspec);

        if (wmode != MeasureSpec.EXACTLY) {
            throw new IllegalArgumentException
                ("MaximumWidthLayout only works for match_parent widths");
        }

        // CUtils.LOGD(TAG, "wspec="+MeasureSpec.toString(wspec));
        int maxw;
        if (m_max_child_width > 0) {
            maxw = m_max_child_width + getPaddingLeft() + getPaddingRight();
        }
        else {
            maxw = 0;
        }

        int cwspec;
        int lo = getPaddingLeft();

        if ((maxw == 0) || (wsize <= maxw)) {
            cwspec = wspec;
        }
        else {
            cwspec = MeasureSpec.makeMeasureSpec
                (maxw, MeasureSpec.EXACTLY);
            lo += (wsize - maxw)/2;
        }
        int targetw =
            MeasureSpec.getSize(cwspec) - getPaddingLeft() - getPaddingRight();

        final int count = getChildCount();
        int maxh = 0;
        //CUtils.LOGD(TAG, "lo="+lo+",targetw="+targetw+
        //            ",maxw="+maxw+",wsize="+wsize+
        //            ",pleft="+getPaddingLeft()+",pright="+getPaddingRight());

        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() == GONE) { continue; }

            measureChild(child, cwspec, hspec);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int cw = child.getMeasuredWidth();
            // CUtils.LOGD(TAG, "c-"+i+" cw="+cw);
            lp.m_x = lo;
            final int extra = targetw - cw;
            if (extra > 0) {
                switch (m_child_gravity) {
                case CG_CENTER:
                    lp.m_x += extra/2;
                    break;
                case CG_RIGHT:
                    lp.m_x += extra;
                    break;
                default:
                    // left - do nothing
                    break;
                }
            }
            // CUtils.LOGD(TAG, "final-lo="+lp.m_x+" (extra="+extra+")");
            int ch = child.getMeasuredHeight()
                + getPaddingTop() + getPaddingBottom();
            if (ch > maxh) { maxh = ch; }
        }

        setMeasuredDimension
            (wsize, resolveSize(maxh, hspec));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() == View.GONE) { continue; }
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            //CUtils.LOGD(TAG, "child width: "+lp.m_x+"->"+
            //            (lp.m_x+child.getMeasuredWidth()));
            child.layout
                (lp.m_x, getPaddingTop(),
                 lp.m_x + child.getMeasuredWidth(),
                 getPaddingTop() + child.getMeasuredHeight());
        }
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p)
    { return p instanceof LayoutParams; }

    @Override
    protected LayoutParams generateDefaultLayoutParams()
    {
        return new LayoutParams
            (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs)
    { return new LayoutParams(getContext(), attrs); }
  
    @Override
    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
    { return new LayoutParams(p.width, p.height); }

    private final int m_max_child_width;
    private final int m_child_gravity;

    public static class LayoutParams extends ViewGroup.LayoutParams
    {
        private int m_x;

        public LayoutParams(Context ctx, AttributeSet attrs)
        { super(ctx, attrs); }

        public LayoutParams(int w, int h)
        { super(w, h); }
    }

    private final static int CG_CENTER = 0;
    private final static int CG_LEFT = 1;
    private final static int CG_RIGHT = 2;
    private final static String TAG =
        CUtils.makeLogTag(CMaximumWidthLayout.class);
}




Java Source Code List

android.util.Base64.java
org.savemypics.android.activity.AActivity.java
org.savemypics.android.activity.ABaseSettingsActivity.java
org.savemypics.android.activity.CAccountInfoListAdapter.java
org.savemypics.android.activity.CFlickrSettingsActivity.java
org.savemypics.android.activity.CSnapfishSettingsActivity.java
org.savemypics.android.activity.CStartActivity.java
org.savemypics.android.authenticator.AAuthenticatorActivity.java
org.savemypics.android.authenticator.CAuthenticationService.java
org.savemypics.android.authenticator.CAuthenticator.java
org.savemypics.android.authenticator.CFlickrLoginHookActivity.java
org.savemypics.android.authenticator.CFlickrWebActivity.java
org.savemypics.android.authenticator.CSelectProviderActivity.java
org.savemypics.android.authenticator.CSnapfishLoginActivity.java
org.savemypics.android.db.CAccount.java
org.savemypics.android.db.CDb.java
org.savemypics.android.db.CLocalImage.java
org.savemypics.android.db.CMap.java
org.savemypics.android.db.CRemoteImage.java
org.savemypics.android.event.AEvent.java
org.savemypics.android.event.CEventBus.java
org.savemypics.android.event.CExceptionEvent.java
org.savemypics.android.glue.ABaseGlue.java
org.savemypics.android.glue.CFlickrGlue.java
org.savemypics.android.glue.CSnapfishGlue.java
org.savemypics.android.receiver.CNetworkChangeReceiver.java
org.savemypics.android.service.CTaskQueue.java
org.savemypics.android.sync.CMediaUtils.java
org.savemypics.android.sync.CSyncAdapter.java
org.savemypics.android.sync.CSyncService.java
org.savemypics.android.sync.CSyncUtils.java
org.savemypics.android.util.CBitmapUtils.java
org.savemypics.android.util.CFileCache.java
org.savemypics.android.util.CLruCache.java
org.savemypics.android.util.CUtils.java
org.savemypics.android.util.CUtils.java
org.savemypics.android.view.CMaximumWidthLayout.java
org.savemypics.android.view.CModularLayout.java
org.savemypics.android.view.CRefreshButton.java
org.savemypics.android.view.CRoundedBitmapView.java
org.savemypics.android.view.CStatusCounterView.java
org.savemypics.plugin.CIOUtils.java
org.savemypics.plugin.CPlugin.java
org.savemypics.plugin.flickr.CFlickrAlbum.java
org.savemypics.plugin.flickr.CFlickrUtils.java
org.savemypics.plugin.snapfish.CSnapfishAlbum.java
org.savemypics.plugin.snapfish.CSnapfishFeed.java
org.savemypics.plugin.snapfish.CSnapfishUser.java
org.savemypics.plugin.snapfish.CSnapfishUtils.java