1.自定义view
/*
* WHAT Confidential.
* OCO Source Materials.
* WHAT Equipment Digital Management.
* © Copyright WanHe Advanced Technology Ltd. 2019.
* The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what has been deposited with the P.R.China Copyright Office.
*/
package com.example.hskeji.utils;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.hskeji.R;
public class TabView extends LinearLayout {
public TabView(Context context) {
super(context);
}
public TabView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public TabView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private TextView tabview_text;// TabView文本内容
private TextView tabview_line;// TabView下划线条
private void initView(Context context){
tabview_text = new TextView(context);
LayoutParams textParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textParams.setMargins(0, ViewUtil.dip2px(context, 20), 0, 0);
tabview_text.setLayoutParams(textParams);
tabview_text.setTextColor(getResources().getColor(R.color.tab_button_text_color_u));
// setTextSize设置字体大小。参数一:字体大小单位;参数二:字体大小号值
tabview_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tabview_text.getPaint().setFakeBoldText(true);// 设置字体加粗
this.addView(tabview_text);
tabview_line = new TextView(context);
LayoutParams lineParams = new LayoutParams(LayoutParams.MATCH_PARENT, ViewUtil.dip2px(context, 5));
lineParams.setMargins(0, ViewUtil.dip2px(context, 20), 0, ViewUtil.dip2px(context, 5));
tabview_line.setLayoutParams(lineParams);
tabview_line.setBackgroundResource(R.color.app_background_color);
this.addView(tabview_line);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.setOrientation(LinearLayout.VERTICAL);
this.setLayoutParams(layoutParams);
}
/**
* @description: 选中或者不选中TabView,设置其字体色和下划线
* @createtime: 2019/1/8 15:36
* @author: surpermind
* @updatetime: 2019/1/8 15:36
* @modifier: surpermind
* @param: isSelect是否选中
*/
public void setSelect(boolean isSelect){
// 处理文字色值
// 处理线条色值
if (isSelect) {
tabview_text.setTextColor(getResources().getColor(R.color.tab_button_text_color_h));
tabview_line.setBackgroundResource(R.color.tab_button_underline_color);
} else {
tabview_text.setTextColor(getResources().getColor(R.color.tab_button_text_color_u));
tabview_line.setBackgroundResource(R.color.white);
}
}
/**
* @description: 设置TabView的Text值
* @createtime: 2019/1/9 15:51
* @author: surpermind
* @updatetime: 2019/1/9 15:51
* @modifier: surpermind
* @param: textValue
*/
public void setTextValue(String textValue) {
if (TextUtils.isEmpty(textValue)) {
textValue = "";
}
tabview_text.setText(textValue);
}
}
/*
* WHAT Confidential.
* OCO Source Materials.
* WHAT Equipment Digital Management.
* © Copyright WanHe Advanced Technology Ltd. 2019.
* The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what has been deposited with the P.R.China Copyright Office.
*/
package com.example.hskeji.utils;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
/**
* @description: 处置屏幕分辨
* @createtime:
* @author: surpermind
* @updatetime:
* @modifier: surpermind
* @version: 1.0
*/
public class ViewUtil {
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)<br>
*/
public static int dip2px(Context context, float dpValue) {
return (int) (dpValue * getDensity(context) + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp<br>
*/
public static int px2dip(Context context, float pxValue) {
return (int) (pxValue / getDensity(context) + 0.5f);
}
/**
* 功能描述:获取当前设备屏幕分辨率<br>
*/
public static float getDensity(Context context) {
return context.getResources().getDisplayMetrics().density;
}
/**
* 功能描述:获得屏幕分辨率<br>
*
* @param context
* @return
*/
public static float getScreenDensity(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.density;
}
/**
* 功能描述:获得屏幕高度<br>
*
* @param context
* @return
*/
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
/**
* 功能描述:获得屏幕宽度<br>
*
* @param context
* @return
*/
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}
/**
* 功能描述:获得状态栏的高度<br>
*
* @param context
* @return
*/
public static int getStatusHeight(Context context) {
int statusHeight = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
return statusHeight;
}
/**
* 功能描述:获取当前屏幕截图,包含状态栏<br>
*
* @param activity
* @return
*/
public static Bitmap snapShotWithStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
view.destroyDrawingCache();
return bp;
}
/**
* 功能描述:获取当前屏幕截图,不包含状态栏<br>
*
* @param activity
* @return
*/
public static Bitmap snapShotWithoutStatusBar(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return bp;
}
}
3.Activity 实现
private void initView() {
linearHome = (LinearLayout) findViewById(R.id.linear_home);
tabHome = (TabView) findViewById(R.id.tab_home);
tabT = (TabView) findViewById(R.id.tab_t);
tabH = (TabView) findViewById(R.id.tab_h);
textHome = (TextView) findViewById(R.id.text_home);
tabHome.setTextValue("首页");
tabT.setTextValue("购物");
tabH.setTextValue("我的");
tabHome.setSelect(true);
tabH.setSelect(false);
tabT.setSelect(false);
tabHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textHome.setText("首页");
tabHome.setSelect(true);
tabH.setSelect(false);
tabT.setSelect(false);
}
});
tabH.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textHome.setText("我的");
tabHome.setSelect(false);
tabH.setSelect(true);
tabT.setSelect(false);
}
});
tabT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textHome.setText("购物");
tabHome.setSelect(false);
tabH.setSelect(false);
tabT.setSelect(true);
}
});
//扫一扫按钮
btnSys = (Button) findViewById(R.id.btn_sys);
btnSys.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:orientation="horizontal"
android:id="@+id/linear_home"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_width="match_parent"
android:layout_height="200dp">
<com.example.hskeji.utils.TabView
android:id="@+id/tab_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View
android:layout_width="50dp"
android:layout_height="50dp"/>
<com.example.hskeji.utils.TabView
android:id="@+id/tab_t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View
android:layout_width="50dp"
android:layout_height="50dp"/>
<com.example.hskeji.utils.TabView
android:id="@+id/tab_h"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="@+id/text_home"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:textSize="30dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="50dp"/>
<Button
android:id="@+id/btn_sys"
android:text="扫一扫"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/text_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>