圖片合成文字
關(guān)鍵: 文字的居中: 測(cè)量文字區(qū)域的寬度
還要注意圖片放置的位置: 那個(gè)drawable下,或者是raw或assert下.不同地方,scale參數(shù)不同,最終生成的圖片大小也不同.
Rect bounds0 = new Rect();
textPaint.getTextBounds(compoundBean.name, 0, compoundBean.name.length(), bounds0);
int x = compoundBean.nameEndX - bounds0.width()-3;
compoundBean.nameStartX = x;
canvas.drawText(compoundBean.name, x, compoundBean.nameEndY-5, textPaint);
全部代碼:
private void compoundIN(PicCompoundBean compoundBean) {
XLogUtil.e(compoundBean.toString());
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap photo = BitmapFactory.decodeResource(getResources(),compoundBean.rawId,options);
int width = photo.getWidth(), hight = photo.getHeight();
System.out.println("寬"+width+"高"+hight);
Bitmap icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); //建立一個(gè)空的BItMap
Canvas canvas = new Canvas(icon);//初始化畫布繪制的圖像到icon上
Paint photoPaint = new Paint(); //建立畫筆
photoPaint.setDither(true); //獲取跟清晰的圖像采樣
photoPaint.setFilterBitmap(true);//過(guò)濾一些
Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());//創(chuàng)建一個(gè)指定的新矩形的坐標(biāo)
Rect dst = new Rect(0, 0, width, hight);//創(chuàng)建一個(gè)指定的新矩形的坐標(biāo)
canvas.drawBitmap(photo, src, dst, photoPaint);//將photo 縮放或則擴(kuò)大到 dst使用的填充區(qū)photoPaint
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//設(shè)置畫筆
textPaint.setTextSize(PicCompoundBean.nameFontSize);//字體大小
//textPaint.setTypeface(Typeface.DEFAULT_BOLD);//采用默認(rèn)的寬度
textPaint.setColor(Color.parseColor("#ea2420"));//采用的顏色
Rect bounds0 = new Rect();
textPaint.getTextBounds(compoundBean.name, 0, compoundBean.name.length(), bounds0);
int x = compoundBean.nameEndX - bounds0.width()-3;
compoundBean.nameStartX = x;
canvas.drawText(compoundBean.name, x, compoundBean.nameEndY-5, textPaint);//繪制上去字淮逻,開始未知x,y采用那只筆繪制
/* canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();*/
Paint textPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//設(shè)置畫筆
textPaint2.setTextSize(PicCompoundBean.codeFontSize);//字體大小
textPaint2.setTypeface(Typeface.DEFAULT_BOLD);//采用默認(rèn)的寬度
//textPaint2.setColor(Color.parseColor("#e72fb8"));//采用的顏色
Rect bounds = new Rect();
//textPaint2.setTextAlign(Paint.Align.CENTER);
textPaint2.getTextBounds(compoundBean.code, 0, compoundBean.code.length(), bounds);
int x2 = (width - bounds.width())/2;
compoundBean.codeStartX = x2;
textPaint2.setShader(new LinearGradient(bounds.left, bounds.top, bounds.right, bounds.bottom, Color.parseColor("#f06776"),Color.parseColor("#e72fb8"), Shader.TileMode.REPEAT));
//textPaint.setShadowLayer(3f, 1, 1,this.getResources().getColor(android.R.color.background_dark));//影音的設(shè)置
canvas.drawText(compoundBean.code, x2, compoundBean.codeStartY-14, textPaint2);//繪制上去字里伯,開始未知x,y采用那只筆繪制
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
mIvContent.setImageBitmap(icon);
isImageSet = true;
bitmapForSave = icon;
photo.recycle();
XLogUtil.e("compoundIN---");
//saveMyBitmap(icon);
}
superAdpter在multiType下的bug:
https://github.com/hss01248/SuperAdapter
item增減時(shí),holder = (SuperLvHolder) convertView.getTag(); 所獲取的holder并非對(duì)應(yīng)type的holder:
修復(fù):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SuperLvHolder holder = null;
if (convertView == null){
holder = generateNewHolder(context,getItemViewType(position));
convertView = holder.rootView;
convertView.setTag(holder);
}else {
holder = (SuperLvHolder) convertView.getTag();
//修復(fù)multitype下的bug:
if(!(holder.type == getItemViewType(position))){
holder = generateNewHolder(context,getItemViewType(position));
convertView = holder.rootView;
convertView.setTag(holder);
}
}
holder.assingDatasAndEvents(context,datas.get(position),position,position == getCount() -1,isListViewFling,datas,this);
return convertView;
}
分享到facebook的messanger:
https://developers.facebook.com/docs/sharing/android
官方文檔對(duì)messanger只有從web端打開app的url,沒(méi)有Android端分享的示例,后來(lái)從Stack Overflow上找到:
其實(shí)與分享到facebook基本一樣,只不過(guò)一個(gè)是用MessageDialog,一個(gè)是用ShareDialog.
if (!MessageDialog.canShow(ShareLinkContent.class)) {
callBack.onFail(new Throwable("MessageDialog.canShow(ShareLinkContent.class) is false"));
return;
}
ShareLinkContent.Builder shareLinkContentBuilder = new ShareLinkContent.Builder()
.setContentTitle(shareInfo.getShareContentTitle())
.setContentDescription(shareInfo.getShareContentDescription())
.setContentUrl(Uri.parse(shareInfo.getShareContentUrl()));
shareLinkContentBuilder.setImageUrl(Uri.parse(shareInfo.getShareImageUrl()));
MessageDialog messageDialog = new MessageDialog(activity);
messageDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result) {
callBack.onSuccess();
}
@Override
public void onCancel() {
callBack.onCancel();
}
@Override
public void onError(FacebookException error) {
callBack.onFail(error);
}
});
messageDialog.show(shareLinkContentBuilder.build());
cookie同步
項(xiàng)目中用到asyhttpclient,retrofit,以及webview,需要做cookie同步.
吐槽:其實(shí)項(xiàng)目中cookie只在登錄時(shí)返回,用于后續(xù)鑒權(quán),且后續(xù)的請(qǐng)求服務(wù)器不再寫cookie. 鑒權(quán)模式的選擇很業(yè)余,不倫不類.
同步方式1: 利用文件實(shí)現(xiàn)同步:
序列化到本地特定文件中,各client都從這個(gè)文件中讀寫cookie.同步方式2:某一client負(fù)責(zé)文件讀寫,其他從這個(gè)client的內(nèi)存中讀取:
由于asyhttpclient和retrofit中Cookie類不一樣,所以需要轉(zhuǎn)換:
private org.apache.http.cookie.Cookie transCookie(Cookie item) {
BasicClientCookie2 cookie = new BasicClientCookie2(item.name(),item.value());
cookie.setPath(item.path());
cookie.setDomain(item.domain());
return cookie;
}
private Cookie transCookie2(org.apache.http.cookie.Cookie item) {
Cookie cookie = new Cookie.Builder()
.domain(item.getDomain())
//.expiresAt(item.getExpiryDate().getTime())
.name(item.getName())
.path(item.getPath())
.value(item.getValue())
.build();
return cookie;
}
okhttpclient讀寫時(shí)拿到AsyHttpclient里的cookie:
//讀cookie
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
try {
List<Cookie> cookies = cookieStore.getCookies();
if(cookies ==null){
cookies = new ArrayList<>();
}
XLogUtil.obj(cookies);
List<org.apache.http.cookie.Cookie> cookiesFromAsyHttp = HttpManager.getInstance().getAllCookie();
//addCookieFromAsyHttp(cookies,cookiesFromAsyHttp);
XLogUtil.obj(cookiesFromAsyHttp);
XLogUtil.obj(cookies);
return cookies ;
} catch (Exception e) {
e.printStackTrace();
}
return new ArrayList<>();
}
//寫cookie
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
try {
if (cookies != null && cookies.size() > 0) {
for (Cookie item : cookies) {
cookieStore.addCookie(item);
org.apache.http.cookie.Cookie cookie = transCookie(item);
//HttpManager.getInstance().getCookieStore().addCookie(cookie);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
webview同步來(lái)自AsyHttpclient里的cookie:
核心:
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, cookie);
private void syncWebViewCookie() {
List<Cookie> cookies = HttpManager.getInstance().getAllCookie();
Context context = MyApplication.getInstance().getApplicationContext();
Html5CookiesManger.syncCookie(context, mUrl, cookies);
}
//Html5CookiesManger
/**
* 將cookie同步到WebView
*
* @param url WebView要加載的url
* @param cookie 要同步的cookie
* @return true 同步cookie成功,false同步cookie失敗
*/
private static boolean syncCookie(Context context, String url, String cookie) {
LoggerUtils.d(TAG, "context" + context);
LoggerUtils.d(TAG, "url" + url);
LoggerUtils.d(TAG, "cookie" + cookie);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
CookieSyncManager.createInstance(context);
}
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, cookie);//如果沒(méi)有特殊需求,這里只需要將session id以"key=value"形式作為cookie即可
XLogUtil.e("h5 cookie2:"+cookie);
String newCookie = cookieManager.getCookie(url);
return !TextUtils.isEmpty(newCookie);
}
public static boolean syncCookie(Context context, String url, List<Cookie> cookies) {
String host = "";
try {
host = new URL(url).getHost().toLowerCase();// 此處獲取值轉(zhuǎn)換為小寫
} catch (MalformedURLException e) {
e.printStackTrace();
}
LoggerUtils.d(TAG, "size = " + cookies.size());
String newCookie = "";
for (Cookie cookie : cookies) {
newCookie = cookie.getName() + "=" + cookie.getValue()
+ ";Domain=" + cookie.getDomain()
+ ";Path=" + cookie.getPath();
XLogUtil.e("h5 cookie:"+newCookie);
syncCookie(context, host, newCookie);
LoggerUtils.d(TAG, "cookie" + cookie);
LoggerUtils.d(TAG, "host" + host);
LoggerUtils.d(TAG, "getDomain" + cookie.getDomain());
}
return syncCookie(context, host, newCookie);
}