消除闪屏白色
//消除闪屏白色
<item name="android:windowDisablePreview">true</item>
跑马灯效果
//跑马灯效果
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
tv.setSelected(true);
loading加载
//loading加载
public class LoadingUtil {
private static View loadingView;
private static boolean flag;
public static void loading(Activity activity){
if (loadingView == null)
loadingView = View.inflate(activity, R.layout.page_loading,null);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (!flag){
activity.addContentView(loadingView,layoutParams);
flag = true;
}
}
public static void unloading(){
if (flag){
CommonUtil.removeSelfFromParent(loadingView);
flag = false;
}
}
}
网络请求
//网络请求
public class HttpCoonUtil {
private static String url = "http://127.0.0.1:7080/transportservice/action/";
public static void doPost(String action, Object obj, Class clazz, Mycallback mycallback, int type) {
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = RequestBody.create(MediaType.get("application/json;chartset=UTF-8"), JSON.toJSONString(obj));
Request request = new Request.Builder().post(requestBody).url(url+action).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) {}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
Log.d("jsonData",str);
Object o = JSON.parseObject(str,clazz);
mycallback.sendResult(o,type);
}
});
}
public interface Mycallback {
void sendResult(Object obj, int type);
}
}
二维码
//二维码
public class ZXingUtil {
private static int width = 800;
private static int height = 800;
public static Bitmap zxing(String context) {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix encode = null;
try {
encode = qrCodeWriter.encode(context, BarcodeFormat.QR_CODE, width, height, hints);
} catch (WriterException e) {
e.printStackTrace();
}
int[] colors = new int[width * height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (encode.get(i,j))
colors[i * width +j] = Color.BLACK;
else
colors[i * width +j] = Color.WHITE;
}
}
return Bitmap.createBitmap(colors,width,height,Bitmap.Config.RGB_565);
}
}
播放视频
//播放视频
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/raw/" + R.raw.test_video);
mVideoView.setVideoURI(uri);
通知
//通知
private void showNotification() {
NotificationManager manager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(getActivity());
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("title");
builder.setContentText("123");
Notification build = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
build = builder.build();
}
manager.notify(1,build);
}
DatePickerDialog
//DatePickerDialog
private void setDate() {
Calendar calendar = Calendar.getInstance();
new DatePickerDialog(getContext(), (datePicker, i, i1, i2) -> {
tvDate.setText(i+"年"+i1+1+"月"+i2+"日");
}, calendar.get(Calendar.YEAR)
, calendar.get(Calendar.MONTH)
, calendar.get(Calendar.DAY_OF_MONTH))
.show();
}
TimePickerView
TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {
tv.setText("<" + getTime(date) + ">");
}
}).isDialog(true).build(); //.isDialog(true)是否为弹窗模式
pvTime.show();
private String getTime(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
}
自定义dialog
//自定义dialog
private void showDialog(){
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_num6,null,false);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext()).setView(view);
alertDialog = builder.create();
alertDialog.show();
alertDialog.setCanceledOnTouchOutside(false);//设置点击外部不可取消
}
MPAndroidChart
//饼图半径
pieChart.setTransparentCircleRadius(0);
pieChart.setHoleRadius(0);
pieDataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);//lable外部展示
pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);//value外部展示
pieDataSet.setValueLinePart1OffsetPercentage(80f);
pieDataSet.setValueLinePart1Length(1f);//
pieDataSet.setValueLinePart2Length(1f);//组成折线的两段折线长短
pieDataSet.setDrawValues(false);
//柱状图上下对比 new float[]{200,200}
yVal.add(new BarEntry(1,new float[]{200,200}));
//柱状图对比图
private ArrayList<IBarDataSet> getBarDataSet() {...}
barChart.groupBars(0.1f,0.1f,0.01f);
HTML
//html文件新建assets创建.html
webview.loadUrl("file:////android_asset/num26.html");
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgress(0);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.show();
webview.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressDialog.cancel();//页面加载完成后progress取消
}
});
广播判断网络连接
public class NetWorkChangeReceiver extends BroadcastReceiver {
boolean flag = true;
@Override
public void onReceive(Context context, Intent intent) {
NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if (networkInfo != null) {
if (NetworkInfo.State.CONNECTED == networkInfo.getState() && networkInfo.isAvailable()) {
Log.e("success", "网络连接" + networkInfo.getState().toString());
flag = true;
} else {
if (flag) {
Log.e("error", "网络未连接");
flag = false;
}
}
}
}
}
<receiver android:name=".util.NetWorkChangeReceiver"/>//别忘了注册广播
//使用
netWorkChangeReceiver = new NetWorkChangeReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
_mActivity.registerReceiver(netWorkChangeReceiver,filter);//注册
if (isRegistered)//解除注册
_mActivity.unregisterReceiver(netWorkChangeReceiver);
动态设置布局宽度
int width = _mActivity.getWindowManager().getDefaultDisplay().getWidth();//屏幕宽
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tvLeft.getLayoutParams();
layoutParams.width = dip2px(_mActivity, width);
tvLeft.setLayoutParams(layoutParams);
高德地图
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
//创建map
map.onCreate(savedInstanceState);
AMap aMap;
if (aMap == null) aMap = map.getMap();
//锚点
LatLng latLng = new LatLng(39.906901, 116.397972);
aMap.addMarker(new MarkerOptions().position(latLng).title("北京").snippet("DefaultMarker"));
//设置地图模式
//public static final int MAP_TYPE_NORMAL = 1;普通模式
//public static final int MAP_TYPE_SATELLITE = 2;卫星模式
//public static final int MAP_TYPE_NIGHT = 3;夜间模式
//public static final int MAP_TYPE_NAVI = 4;导航模式
//public static final int MAP_TYPE_BUS = 5;交通模式
aMap.setMapType(AMap.MAP_TYPE_SATELLITE);
Recyclerview
Num25Adapter2 adapter2 = new Num25Adapter2(bean.getSites(), (tv, tv2, position) -> {
View view = rv.getLayoutManager().findViewByPosition(lastposition);//找到上次点击的item并设置成原来的颜色
TextView tv_site = view.findViewById(R.id.tv_site);
TextView tv_num = view.findViewById(R.id.tv_num);
tv_site.setTextColor(Color.BLACK);
tv_num.setTextColor(Color.BLACK);
tv_num.setBackgroundResource(R.drawable.round_white);
//设置新点击的颜色
tv2.setTextColor(Color.RED); tv.setBackgroundResource(R.drawable.num25_shape);
tv.setTextColor(Color.WHITE);
lastposition = position; //此次点击的位置放入历史位置中
});
rv.setItemViewCacheSize(30);//设置缓存
网友评论