2017年7月14日星期五

android 根据版本设置语言

//下面这是修改语言 方法
getActivity().onConfigurationChanged(AppUtil.setLocale(getActivity()));


public static Locale getLocale(Context context) {    String language = PreferencesUtil.getValue(context, CommonConstants.PREFERENCE_SELECT_LANGUAGE, Locale.ENGLISH.toString());
    Locale locale = new Locale(language);    if (language.contains("_")) {        locale = new Locale(language.substring(0,
                language.indexOf("_")), language.substring(                language.indexOf("_") + 1, language.length()));    }
    return locale;}
public static Configuration setLocale(Context context) {
    Locale locale = getLocale(context);    Log.e("locale  isSet " + locale.toString(), "save Language = " + language);    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {        return updateResources(context, locale);    }    return updateResourcesLegacy(context, locale);}
@TargetApi(Build.VERSION_CODES.N)private static Configuration updateResources(Context context, Locale language) {    Resources res = context.getResources();    Locale.setDefault(language);    Configuration configuration = context.getResources().getConfiguration();    configuration.setLocale(language);    res.updateConfiguration(configuration, res.getDisplayMetrics());    return configuration;}
@SuppressWarnings("deprecation")private static Configuration updateResourcesLegacy(Context context, Locale language) {    Locale.setDefault(language);    Resources resources = context.getResources();    Configuration configuration = resources.getConfiguration();    configuration.locale = language;    resources.updateConfiguration(configuration, resources.getDisplayMetrics());    return configuration;}

2017年6月28日星期三

国际化语言设置


在下面中 local是国家代码, region是地区代码. region可有可无.比如汉语的话可能有繁体,跟简体.这种时候就需要region的代码了.在国际化语言设置的时候 这个可以根据 values当中的国际化语言参考设置.比如 values-ex-rMX. 这个是 西班牙的墨西哥语言. 所以 local就成了 es(西班牙), region成为了
mx(南美语).

        Locale locale = new Locale(local, region);
            Configuration config = new Configuration();            config.locale = locale;            App.getAppContext().getResources().updateConfiguration(config, App.getAppContext().getResources().getDisplayMetrics());


除了这个再看看 java中日期的国际化设置.
也是很简单.都是使用 Locale的.
但需要注意的是 比如你想对 月进行国际化翻译的话 'MMM'.一般是2个M,但如果是3个M的话
他就会翻译成国际化的语言.比如JUN

Locale locale = new Locale(language, countryCode);

SimpleDateFormat fromFormat = new SimpleDateFormat("yyyyMMdd");SimpleDateFormat newFormat  = new SimpleDateFormat("yyyyMMMdd", locale);
Date startDate = null;try{    startDate = fromFormat.parse("20170604");}catch(ParseException e){    e.printStackTrace();}return newFormat.format(startDate);

2017年5月24日星期三

paint之setXfermode.根据图片轨迹把图片画上去

本篇重点是: 根据设置的图片轨迹之上画上去资源图片.
比如下面: 可以看到他的框架是六角形.那么放上去图片时会根据那个轨迹贴上去.


研究一下网址.对理解有帮助
http://blog.csdn.net/harvic880925/article/details/51264653

核心代码是:
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

使用方法:
其中setImageFrame是 setImageMask是轨迹用的框.
setImageFrame是 边框, setImageResource是要根据轨迹画上去的 资源图片

mIvTeamImage.setImageMask(R.drawable.join_a_team_teamlogo_frame);//// TODO: 2017. 5. 24. 이미지를 올려놓아야 한다mIvTeamImage.setImageFrame(R.drawable.bg_team_image_324_362);
mIvTeamImage.setImageResource(R.drawable.teamlogo_no_img);

贴出全部代码.

public class MaskImage extends ImageView {    Bitmap mMask        = null;    Bitmap mFrame       = null;    int mFrameResId     = -1;
    Bitmap mOriBmp      = null;    //Bitmap mResult;    private int m_nWidth            = -1;    private int m_nHeight           = -1;    private int m_nResId            = -1;    private String m_szPath         = null;    private boolean m_bNeedRemask   = false;
    private Canvas mCanvas = null;
    public MaskImage(Context context) {        super(context);
    }
    public MaskImage(Context context, AttributeSet attrs) {        super(context, attrs);    }
    public MaskImage(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }
    public void setImageMask(int resource) {        if(mMask != null) {            mMask.recycle();        }        mMask = BitmapFactory.decodeResource(getResources(), resource);    }

    public void setImageFrame(int resource) {        if(mFrameResId == resource)            return;
        if(mFrame != null) {            mFrame.recycle();        }        if (resource == 0) {            mFrame = null;            mFrameResId = -1;            return;        }        mFrameResId = resource;        mFrame = BitmapFactory.decodeResource(getResources(), resource);        m_bNeedRemask = true;    }

    @Override    public void setImageResource(final int resId) {        if(m_nResId == resId) {            return;        }        m_bNeedRemask = true;        m_nResId = resId;        m_szPath = null;        mOriBmp = null;        invalidate();    }
    @Override    public void setImageBitmap(Bitmap bm) {        mOriBmp = bm;        m_nResId = -1;        m_szPath = null;        m_bNeedRemask = true;        invalidate();    }
    public void setImagePath(String imagePath) {        if(m_szPath != null && m_szPath.equals(imagePath)) {            return;        }
        mOriBmp = null;        m_nResId = -1;        m_szPath = imagePath;        m_bNeedRemask = true;        invalidate();    }

    private void  createMaskBitmap(Bitmap original)    {        if(mMask != null) {            if(mMask.getWidth() != m_nWidth || mMask.getHeight() != m_nHeight) {                Bitmap bmp = Bitmap.createScaledBitmap(mMask, m_nWidth, m_nHeight, true);                mMask.recycle();                mMask = bmp;            }        }
        if(mFrame != null) {            if(mFrame.getWidth() != m_nWidth || mFrame.getHeight() != m_nHeight) {                Bitmap bmp = Bitmap.createScaledBitmap(mFrame, m_nWidth, m_nHeight, true);                mFrame.recycle();                mFrame = bmp;            }        }
        if(original != null) {            if(original.getWidth() != m_nWidth || original.getHeight() != m_nHeight) {                Bitmap bmp = Bitmap.createScaledBitmap(original, m_nWidth, m_nHeight, true);                original.recycle();                original = bmp;            }        }
        Bitmap bitmap = Bitmap.createBitmap(m_nWidth, m_nHeight, Config.ARGB_8888);        Canvas mCanvas = new Canvas(bitmap);        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));        if(original != null) {            mCanvas.drawBitmap(original, 0, 0, null);        }        mCanvas.drawBitmap(mMask, 0, 0, paint);        paint.setXfermode(null);        if (null != mFrame) {            mCanvas.drawBitmap(mFrame, 0, 0, paint);        }        super.setImageBitmap(bitmap);    }
    @Override    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {        super.onLayout(changed, left, top, right, bottom);        if(changed && m_nWidth == -1) {            m_nWidth = right - left;            m_nHeight = bottom - top;        }    }
    @Override    protected void onDraw(Canvas canvas) {        if(m_bNeedRemask) {            Bitmap bmp = null;            if(m_nResId != -1) {                bmp = BitmapFactory.decodeResource(getResources(), m_nResId);            } else if(m_szPath != null) {                bmp = BitmapFactory.decodeFile(m_szPath);            } else if(mOriBmp != null)           {                bmp = mOriBmp;            }            createMaskBitmap(bmp);            m_bNeedRemask = false;        }        super.onDraw(canvas);    }}