获取资源ID

时间:2023-03-09 00:20:33
获取资源ID

比如,设置一张gif图片的宽高

gif.setShowDimension((int) CommonUtil.getDimen(R.dimen.gif),
(int) CommonUtil.getDimen(R.dimen.gif_height));

import java.net.URLEncoder;

import android.content.res.Resources;
import android.graphics.drawable.Drawable; import com.amap.api.location.core.CoordinateConvert;
import com.amap.api.location.core.GeoPoint;
import com.amap.api.maps.model.LatLng;
import com.etoury.etoury.global.BaseApplication;
/**
* 基础工具类
* @author shaoze
*
*/
public class CommonUtil {
/**
* 在主线程执行Runnable
* @param r
*/
public static void runOnUIThread(Runnable r){
BaseApplication.getHandler().post(r);
}
/**
* 获取Resource对象
* @return
*/
public static Resources getResources(){
return BaseApplication.getContext().getResources();
} /**
* 获取字符串的资源
* @param resId
* @return
*/
public static String getString(int resId){
return getResources().getString(resId);
} /**
* 获取字符串数组的资源
* @param resId
* @return
*/
public static String[] getStringArray(int resId){
return getResources().getStringArray(resId);
} /**
* 获取图片资源
* @param resId
* @return
*/
public static Drawable getDrawable(int resId){
return getResources().getDrawable(resId);
}
/**
* 获取dp资源
* @param resId
* @return
*/
public static float getDimen(int resId){ return getResources().getDimension(resId);
} /**
* 获取颜色资源
* @param resId
* @return
*/
public static int getColor(int resId){
return getResources().getColor(resId);
}
/**
* url 编码转码
* @param url
* @return 已编码url
* @throws Exception
*/
public static String convertUrl(String url) throws Exception{
String chinese = url.substring(url.lastIndexOf("/")+1, url.length());
String header=url.substring(0, url.lastIndexOf("/")+1); String encodeChinese = URLEncoder.encode(chinese, "UTF-8"); return header+encodeChinese;
}
public static LatLng getCorrectlocation(double latitude,double longitude){
GeoPoint pos = CoordinateConvert.fromGpsToAMap(latitude, longitude); LatLng location = new LatLng(pos.getLatitudeE6() * 1.E-6, pos.getLongitudeE6() * 1.E-6);
return location; } }