Friday, October 9, 2020

An easy way to prevent sliding operations in android custom View from exceeding the boundary

 In android development, sliding operations are often used in custom views, and you must prevent sliding beyond the boundary. Here is a very easy way to prevent slipping out of the boundary.

Calculate the value of the left and right border X or the upper and lower border Y. If it does not exceed the screen, the left and top borders are 0, and the right and bottom borders are the width and height of the screen. Then rewrite the scrollTo method:

@Overridepublic void scrollTo(int x, int y) {    

    ​If(x>mRightOffsetX) x=mRightOffsetX;   

    ​ if(x<mLeftOffsetX) x=mLeftOffsetX;   

    ​ super.scrollTo(x, y);

}

Tuesday, October 6, 2020

ViewConfiguration.getMaximumFlingVelocity() is obsolete

 The ViewConfiguration.getMaximumFlingVelocity() method is obsolete. The alternative method is getScaledMinimumFlingVelocity().

usage:

ViewConfiguration.get(getContext()).getScaledMinimumFlingVelocity()

Monday, September 28, 2020

Use DateFormatSymbols().getShortWeekday to convert the dayOfWeek of the calendar into Chinese form

 In android development, sometimes we need to convert the week information obtained from calendar.get(Calendar.DAY_OF_WEEK) into the column format of Chinese "Monday, Tuesday".

The common way is:

string [] Day=new string []{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

Day[dayofweek];

In fact, there is an easier way:

new DateFormatSymbols().getShortWeekdays()[calendar.get(Calendar.DAY_OF_WEEK)]

This method is multi-language support, and it will be automatically converted to the corresponding form when switching languages.




Linearlayout scrollbars = "vertical" is not useful, the solution to Linearlayout scrolling

The content of Linearlayout often exceeds the screen. Although the scrollbars=”vertical” parameter can be set, it is useless.

The correct approach is to nest a layer of ScrollView outside the LinearLayout.

<ScrollView xmlns:android=”http://schemas.android.com/apk/res/android”

    android:layout_width=”match_parent”

    android:layout_height=”match_parent”

    android:fadingEdge=”vertical”

    android:scrollbars=”vertical”>

    <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

        android:layout_width=”match_parent”

        android:layout_height=”match_parent”

        android:orientation=”vertical”>

            android:layout_width=”match_parent”

            android:layout_height=”wrap_content”

            android:text="Please select a date:"

            android:textColor=”@android:color/holo_blue_dark”

            android:textSize=”22sp” />

Saturday, September 26, 2020

Android custom view only receives ACTION_DOWN solution

 Insert picture description here

When customizing View in Android development, the dispatchTouchEvent method is often rewritten, but in some cases, only ACTION_DOWN can be obtained, but ACTION_MOVE and ACTION_UP cannot be obtained.

The solution to the custom view receiving only ACTION_DOWN is
to add the following sentence in the custom View constructor:

setClickable(true);

Or add the following parameters to the layout:

android:clickable="true"

Or override the onTouchEvent method and return true.

@Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

So you can get the response of ACTION_MOVE and ACTION_UP in the dispatchTouchEvent method.

As for the principle, I am not sure, and I don't want to figure it out. There are too many contents in this android system and too many pits. You can only encounter problems and solve them, otherwise the development progress will be too slow!

Get system attributes in android custom View

We all know that in android development, the method of obtaining custom parameters of custom View is:


TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.Horiz);


We can get these custom parameters:


app:defaultValue="5"


app:valueFrom="2"


app:valueTo="10"


But sometimes we want to get the parameters of the system View itself, like this:


android:layout_width="200dp"


android:textSize="32sp"


android:columnCount="3"


In fact, the acquisition method is still like this:


TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.Hori);


Just refer to the "android:" namespace when defining attrs.


<?xml version="1.0"encoding="utf-8"?>


<resources>


<declare-styleable name="Horiz">


<attr name="android:textSize"/>


<attr name="android:textColor"/>


<attr name="android:defaultValue"/>


<attr name="android:valueFrom"/>


<attr name="android:valueTo"/>


<attr name="android:columnCount"/>


</declare-styleable>


</resources>


In this way, you can directly use the commonly used android naming when layout custom View.


<com.xiaoyifei.horizo


android:id="@+id/id_horizontal_picker"


android:layout_width="200dp"


android:background="#000000"


android:textSize="32sp"


android:textColor="@android:color/white"


android:defaultValue="5"android:valueFrom="2"


android:valueTo="10"


android:columnCount="3"


android:layout_height="50dp"/>

Friday, September 25, 2020

About Android Studio debugging: abnormal E/HAL:load:id=gralloc!=hmi->id=gralloc solution

 In the development process of Android program, sometimes it just flashes when debugging the program, and only an exception E/HAL:load:id=gralloc!=hmi->id=gralloc is left in the log. It is difficult to find out the reason by looking at the code, which is very annoying.



This problem is the reason why the phone debug log is not opened!

Open the debugging log of the mobile phone and enter the project menu of the mobile phone. The method of entering the project menu is different for each mobile phone. Please Baidu.

The debugging phone I use is Huawei p8, and the way to enter the project menu is: input *#*#2846579#*#* in the dial keyboard.

After entering the project menu-enter the background settings-enter the Log settings-select AP log.


In this way, you can see the specific cause of the problem in the Logcat window of Android Studio.
The error is pointed out in every line of the program.


Thursday, May 21, 2020

Crazy helmet: I earned 8 million in 10 days

On April 21, the Traffic Management Bureau of the Ministry of Public Security issued a notice to deploy the "One Helmet and One Belt" security guardian operation nationwide: From June 1, the Public Security Traffic Management Department will strengthen law enforcement management, investigate and correct motorcycles, Electric bicycle riders do not wear safety helmets and car drivers do not use seat belts.
One month after the news came out, the electric car helmet “fired”. Although the Ministry of Public Security ’s Traffic Management Bureau said yesterday, the scope of law enforcement punishment from June 1 is temporarily only for riding a motorcycle without wearing a safety helmet, but on the e-commerce platform The helmet of an electric car has long been difficult to find, and the price has risen several times. The opposite is the lack of helmet quality and industry standards.
In response, Dongfang.com reporters contacted Zhang Min (pseudonym), a helmet workshop owner in Wenzhou, Zhejiang. Although he declined the reporter ’s request for an interview, he sent a few paragraphs with pride. Produce live video, and must answer questions. The following is his readme:
"I don't understand this at all. I just saw the news and invested money to start (helmet)." Zhang Min revealed, "This wave of 2 million capital has returned, and the net profit has made at least 8 million . "
Zhang Min admitted that there is almost no threshold for the production of electric vehicle helmets. He only needs to pay for OEMs from certified manufacturers. "The state has no clear quality regulations for electric vehicle helmets. As long as the raw materials such as FRP are environmentally friendly, the other party will let me OEM. "
Crazy helmet: I made 8 million in 10 days
(Picture: Unpainted helmet placed in Zhang Min's factory)
Two machines made 8 million in 10 days
Zhang Min is a native of Wenzhou and had a mask business early this year. More than ten days ago, he saw the news of "one helmet and one belt". "I thought at the time, this was an opportunity that I couldn't miss. I immediately bought two stamping machines." Zhang Min said.
According to him, this is like doing mask production before. The fight is at a speed. "Now the price is rising day by day. The supply is in short supply. When there are more people doing it, the price will go down. I guess our place has already opened. There are hundreds of them, and helmets are not consumables, and the heat will certainly not be as good as masks. "
Zhang Min said that at the beginning, he invited the workers to learn and do now, because the operation is not skilled, the two molding machines can only produce 10,000 to 20,000 helmets a day, and then slowly increase, now there are about 50,000 per day Top output.
"Now every day from morning to night, all kinds of calls come in and ask me to ask for a helmet. A family can get 10,000 from me, which is enough for them to be happy." Zhang Min said.
Crazy helmet: I made 8 million in 10 days
(Picture: Helmets stacked in Zhang Min's plant)
Zhang Min said that according to his understanding, the original factory price of such a helmet was only eight or nine yuan, but now it has risen to fifty or sixty yuan. Not only are the wholesale prices of helmets rising, but also the prices of raw materials such as helmet molding machines, molds, and FRP. Zhang Min said that a former molding machine was about 100,000 yuan. "I bought it early, and it only rose to more than 200,000 units. Now I have six or seven hundred thousand. I can't buy it anymore."
"But the more expensive is the mold, a set of 800,000." Zhang Min simply calculated, this workshop initially invested nearly 2 million, according to the cost provided by Zhang Min and the selling price, the reporter helped him calculate the pen : About 15,000 tops were sold every day for the first three days, with a net profit of about RMB 20 each, and about 50,000 tops were sold every day for the next seven days, with a net profit of about RMB 30 per head. After accounting, the investment cost of RMB 2 million was removed, and the net profit About 9.4 million yuan.
Zhang Anmin expressed his affirmation of this figure, "After ten days, I have already paid back my money and deducted some bits and pieces of money. The net profit should be no less than 8 million."
"It's all unlicensed and OEM production ... there is no requirement for the hardness of the material"
Zhang Min admitted that his workshop does not have any production qualifications and belongs to "unlicensed operation", but he only needs to pay the certified manufacturers to affix their trademarks.
"For example, like a factory certified by the DOT (US Department of Transportation), one is about three to five yuan for them. In fact, the country does not have clear quality regulations for electric vehicle helmets. I use glass fiber reinforced plastic. No authorization is required for authorization. "
According to Zhang Min's understanding, there are many such OEM operations around him. "No way, the market is too hot. I want to find opportunities to make quick money."
Yesterday afternoon, a number of Taobao helmet sellers told Dongfang.com news reporter that the price of the helmet will not be reduced in a short time, but the possibility of rising is very high.
Crazy helmet: I made 8 million in 10 days
Online prices have risen sharply, and supply is in short supply. The sale of offline helmets is also hot. Yesterday afternoon, longitudinal news reporters visited two offline electric car shops in Minhang District and Songjiang District, Shanghai. Both bosses said that it is difficult to get the helmet at present, and the price has more than doubled compared with the past.
The boss driving on Xinzhu Road in Minhang District said, "Now let's not choose colors, it would be good to have a helmet." She said that she only has two helmets here, and one has no brand, about 80 yuan. The other is a branded helmet, about 130 pieces. However, the reporter noticed that there is no other product quality certification label on the helmet except for the certificate printed by the manufacturer.
Regarding the quality of the helmet, the boss who drove on Chenchun Road in Songjiang District also said: "Whoever will give it a 3C certificate for something over a hundred pieces, but it must be good."
Deputies to the National People's Congress called for the formulation of mandatory national standards for electric bicycle helmets as soon as possible
It is understood that the number of electric bicycles in China is nearly 300 million, but compared with motorcycle helmets that already have quality standards, electric bicycle helmets that lack national industry standards have always received much attention due to uneven quality.
On May 20, Xi Wen, a representative of the National People's Congress, called on Beijing to formulate mandatory national standards for electric bicycle occupant helmets as soon as possible. The lack of standards resulted in low-quality and low-price competition in electric bicycle helmets.
Crazy helmet: I made 8 million in 10 days
(Picture: Xi Wen, data map
Representative Xi Wen introduced that he began to investigate the chaos in the production and sales of electric bicycle helmets in July 2019, and found that it is relatively common for companies to produce helmets using inferior waste materials. "I bought multiple helmets with high sales volume from the market and sent them for inspection. As a result, they all failed to meet the standard safety protection requirements of the existing helmets, and some even shattered upon collision."
Standard gaps have also led to embarrassment of quality supervision. Representative Xi Wen said that because there is no national or industry standard, the supervisors cannot judge whether the product is qualified or unqualified, and it is more difficult to investigate the production and operation of inferior helmets. "The punishment is insufficient, and the deterrent to the enterprise is also small, and the bad currency expels the good currency."
In addition to the appeal of the NPC deputies, the Ministry of Public Security also took action! The official WeChat of the Transportation Bureau of the Ministry of Public Security said in a letter yesterday that it would strictly investigate the price violations and cut off the illegal chain of driving up the price of the helmet.
At the same time, it is clear that from June 1, the scope of law enforcement punishment is limited to traffic violations that do not wear safety helmets when riding motorcycles and do not use seat belts when driving cars.
Continue to carry out propaganda and guidance on the behavior of riding electric bicycles without wearing a safety helmet.