Android screen adaptation scheme

Source: Internet
Author: User
Tags benchmark

Once looked at the Android screen adaptation scheme, there are very many. One of their own use is: first find a mainstream resolution of the Android, such as: 1080*1920 resolution to do benchmark, and then on this benchmark. Adjust all layouts. Image. Mobile phones with other phone resolutions. Adjusted in percent. For example: In 480*800 's mainstream mobile phone, wrote a height=520, then on 480*800 's mobile phone, the height of the control is

480height = 520 * Screen Height/1920. This scheme does a screen fit.


The project encountered a problem.

In the remaining space. The 1080*1920 on the phone is very good, but in the 480*800 low-resolution phone display is incomplete.

View the reason: because my picture resource is 240*240.

The width and height of the imageview of the layout are self-adapting.

So it causes the picture to be too large. And the rest of the fonts, the picture can not be completely displayed. So there are 2 ways to solve the problem: one is to do a low-resolution resource map. The second is the outer nesting of ScrollView controls, which let them slide to show parts that cannot be completely left.


Dynamic layout first to get the width of the XML control:

A few days ago. There is a problem when you define a control. is how to get the high width of the control you define. In the constructor of your own definition control class, you would have thought it would be easy to get. But that's not the case.

I tested the following code:
First, the layout code:
<com.lml.getvalues.myview
android:id= "@+id/myview"
android:layout_width= "Match_parent"
android:layout_height= "150px"
android:background= "#ff0000"/>
again the code for the MyView constructor:
Public MyView (context context, AttributeSet attrs) {
Super (context, attrs);
a= "In the MyView constructor: measuredwidth:" +this.getmeasuredwidth () + ";" + "Measuredheight:" +this.getmeasuredheight () + ";"
+ "Width:" +this.getwidth () + ";" + "Height:" +this.getheight () + "\ n";
String h= "", w= "";
for (int i =0; i < Attrs.getattributecount (); i++) {
if ("Layout_height". Equals (Attrs.getattributename (i))) {
H=attrs.getattributevalue (i);
}else if ("Layout_width". Equals (Attrs.getattributename (i))) {
W=attrs.getattributevalue (i);


b= "In the constructor attrs: width:" +w+ ";" + "Height:" +h+ "\ n";



compile to get A= "in MyView constructor: measuredwidth:0; measuredheight:0; width:0; Height:0 ".
b= "in constructor attrs: width:-1;height:150.0px

the results show that when the width is a value such as match_parent. Just show-1 and so on. Not to meet my needs.



Then I try to get a high width in the oncreate of the corresponding activity. All that was achieved was 0. But I added a click Control in OnCreate to get the high-width event, which can get the height width correctly.

I looked up the information on the Internet, because the control has not been measured in oncreate, so the access is definitely 0. Online there are three methods, methods such as the following:
method One, add the following code in the OnCreate, for example:
int w = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED);
Myview.measure (W, h);
int height = myview.getmeasuredheight ();
int width = myview.getmeasuredwidth ();
Tvvalues.append ("Method one: Height:" +height + ", Width:" + width+ "\ n");

method Two can be implemented, code such as the following:
viewtreeobserver Vto2 = Myview.getviewtreeobserver ();
Vto2.addongloballayoutlistener (New Ongloballayoutlistener () {
@Override
Public void Ongloballayout () {
myview.getviewtreeobserver (). Removeglobalonlayoutlistener (this);
Tvvalues.append ("Method two: Height:" +myview.getheight () + ", Width:" + myview.getwidth () + "\ n");

});
but I found out that Removeglobalonlayoutlistener was obsolete at API level 16, assuming it was removed. The system is read multiple times.

take another look at method three. The code is as follows:

viewtreeobserver vto = Myview.getviewtreeobserver ();
Vto.addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () {
Public Boolean Onpredraw () {
myview.getviewtreeobserver (). Removeonpredrawlistener (this);
int height = myview.getmeasuredheight ();
int width = myview.getmeasuredwidth ();
Tvvalues.append ("method Three: Height:" +height + ", Width:" + width + "). \ n ");
return true;

});
I have joined Myview.getviewtreeobserver () on the basis of the information on the Internet. Removeonpredrawlistener (this), which ensures that the system executes once.

_____________________________________________________________________________________________________________


Problems: (1) Use the above 2 callback listener functions. The listener function is always called and cannot be closed. And the screen jumps will appear splash screen phenomenon. So I gave up.

public void Setactionbar () {Viewtreeobserver observer = Mscrollview.getviewtreeobserver ();//Observer.addonpredrawl Istener (New Viewtreeobserver.onpredrawlistener () {//@Override//public boolean Onpredraw () {//int i = 0 ;//LOG.E ("Homefragment:" + (i++));////int scrollheight = Mscrollview.getchildat (0). GetHeight ();//I NT ScrollHeight = Getscreenheight ()-Getactionview (). GetHeight ()-Mhomead.getheight ()-Mminecare.getheight ()- mainui.bottomheight;//int TextHeight = mhomeprogramtxt.getheight ()////if (ScrollHeight > (bitmapheight+ TextHeight) {////large screen//int firstbarheight = Mfirstbar.getheight ();//int blankheight = Scrol lheight-firstbarheight*2;////linearlayout.layoutparams params = (linearlayout.layoutparams) mFirstBar.getLayou          Tparams ();//Params.setmargins (0, BLANKHEIGHT/3, 0, 0);//Mfirstbar.setlayoutparams (params);//// Linearlayout.layoutparams secOndparams = (linearlayout.layoutparams) msecondbar.getlayoutparams ();//Secondparams.setmargins (0,blankHeight/3,0 , 0);//Msecondbar.setlayoutparams (secondparams);//}//Mscrollview.getviewtreeobserver (). REMOVEONPR Edrawlistener (this);//Return true;//}//});

(2) Choose to use the first method. Suppose the XML is wrap_content wide and can be used to get the value of the control using the Getmeasureheight () method, but the value of the dynamic setting only remembers the value itself.

 public void Set () {int w = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED);    int h = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED);    Getactionview (). Measure (W, h);    Mminecare.measure (W,H);    Detailed values are individually assigned int homeadheight = 520 * Getscreenheight ()/1920; int scrollheight = Getscreenheight ()-Getactionview (). Getmeasuredheight ()-Homeadheight-mminecare.getmeasuredheight    ()-mainui.bottomheight;    Mhomeprogramtxt.measure (W,H);    int TextHeight = Mhomeprogramtxt.getmeasuredheight ();      if (ScrollHeight > (bitmapheight+textheight) *2.5) {//Large screen mfirstbar.measure (w,h);      int firstbarheight = Mfirstbar.getmeasuredheight ();      int blankheight = scrollheight-firstbarheight*2;      Linearlayout.layoutparams params = (linearlayout.layoutparams) mfirstbar.getlayoutparams ();      Params.setmargins (0, BLANKHEIGHT/3, 0, 0);      Mfirstbar.setlayoutparams (params);    Msecondbar.setlayoutparams (params); }  }


Achieve low resolution adaptation effects:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">


High resolution Effect:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

Android screen adaptation scheme

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.