This article shares the specific code for Android nine-grid picture display for your reference. The specific content is as follows
– Main page –
//There is only one Button in the layoutpublic class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); findViewById().setOnClickListener(new () { @Override public void onClick(View view) { //The purpose is to start the Service to open the suspended form startService(new Intent(, )); finish(); } }); } }
—Service opens the floating form —-
/** * description: Open FloatView through Service * Author: ldm * Time: 20162016/8/17 14:03 * Email: 1786911211@ */ public class FloatService extends Service { @Override public void onCreate() { (); (this).showFloatViewOnWindow(); } @Override public IBinder onBind(Intent intent) { return null; } }
—Suspended Form Management Tool Class —-
/** * description: * Author: ldm * Time: 20162016/8/17 11:57 * Email: 1786911211@ */ public class CustomViewManager { //Context private Context mContext; //Example of this class private static CustomViewManager instance; //Custom FloatView private FloatView mFloatView; //Window Management private WindowManager mWindowManager; private CustomViewManager(Context context) { = context; mFloatView = new FloatView(mContext); mWindowManager = (WindowManager) (Context.WINDOW_SERVICE); } /** * @param * @description Get instance object through singleton mode * @author ldm * @time 2016/8/17 11:59 */ public static CustomViewManager getInstance(Context mContext) { if (null == instance) { synchronized () { if (null == instance) { instance = new CustomViewManager(mContext); } } } return instance; } /** * @param * @description Display custom FloatView on mobile screen * @author ldm * @time 2016/8/17 13:47 */ public void showFloatViewOnWindow() { parmas = new (); = (); = (); //The window pattern placement position = | ; // If the gravity property is ignored, it indicates the absolute X position of the window. = 0; //If the gravity property is ignored, it indicates the absolute Y position of the window. = 0; //// Phone window. It is used for telephone interactions (especially incoming calls). It is placed above all applications, under the status bar. = .TYPE_SYSTEM_ALERT; //FLAG_NOT_FOCUSABLE prevents the window from gaining focus, so that the user can't send key events and button events to the window quickly. //FLAG_NOT_TOUCH_MODAL Even if the window is available for focus, it still sends any event outside the window to other windows after the window. = .FLAG_NOT_FOCUSABLE; // Desired bitmap format. The default is opaque. refer to. = PixelFormat.RGBA_8888; (mFloatView, parmas); } }
—Custom FloatView ——
/** * description: * Author: ldm * Time: 20162016/8/17 11:17 * Email: 1786911211@ */ public class FloatView extends View { //The width of the floating ball private int floatWidth = 150; //The height of the floating ball private int floatHeight = 150; //Floating ball brush private Paint mPaint; //Draw a text brush private Paint mTextPaint; private String text = "50%"; public FloatView(Context context) { super(context); initPaint(); } public int getFloatWidth() { return floatWidth; } public int getFloatHeight() { return floatHeight; } public FloatView(Context context, AttributeSet attrs) { super(context, attrs); initPaint(); } public FloatView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initPaint(); } /** * @param * @description Initialize the brush * @author ldm * @time 2016/8/17 11:37 */ private void initPaint() { //Set the floating ball brush mPaint = new Paint(); (); (true); (true); //Set text brush mTextPaint = new Paint(); (25); (true); (); (true); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //Set the size setMeasuredDimension(floatWidth, floatHeight); } /** * @param * @description Draw the pattern * @author ldm * @time 2016/8/17 11:44 */ @Override protected void onDraw(Canvas canvas) { (canvas); //Draw the floating ball (floatWidth / 2, floatHeight / 2, floatWidth / 2, mPaint); //Draw text metrics = (); //Calculate the text size and refer to: ./3826268/871765/ float textWidth = (text); float x = floatWidth / 2 - textWidth / 2; float dy = -( + ) / 2; float y = floatHeight / 2 + dy; (text, x, y, mTextPaint); } }
Finally, don't forget to add permissions in:
<!--Add permissions--> <uses-permission android:name=".SYSTEM_ALERT_WINDOW" />
Of course, remember to add Service components
<service android:name="." />
After some mobile phones are running, they find that the floating form we want does not appear. You can enter the mobile phone settings center, click App Settings, and turn on the corresponding settings switch of the floating form in the specified application permission settings.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.