android - The app screen is smaller on my phone -
i have made snake app, uses canvas:
the xml layout:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="bottom" android:orientation="vertical" > <framelayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <linearlayout android:id="@+id/surface" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_weight="1" > </linearlayout> <linearlayout android:id="@+id/gameover" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/gameover" android:orientation="vertical" android:visibility="gone" android:layout_gravity="center" android:gravity="center" > <textview android:id="@+id/scoretv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="would save it?" /> <edittext android:id="@+id/name" android:hint="enter name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestfocus /> </edittext> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" > <button android:id="@+id/yes" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="save" /> <button android:id="@+id/no" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="no tnx, menu" /> </linearlayout> </linearlayout> <linearlayout android:id="@+id/stoped" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/gameover" android:orientation="vertical" android:visibility="gone" android:layout_gravity="center" android:gravity="center" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="do you want leave?" /> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" > <button android:id="@+id/continue" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="continue game" /> <button android:id="@+id/leave" style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="leave menu" /> </linearlayout> </linearlayout> </framelayout> <relativelayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginend="0dp" android:layout_gravity="bottom" > <imageview android:id="@+id/right" android:layout_width="50dp" android:layout_height="50dp" android:layout_aligntop="@+id/down" android:layout_torightof="@+id/down" android:src="@drawable/right" /> <imageview android:id="@+id/left" android:layout_width="50dp" android:layout_height="50dp" android:layout_aligntop="@+id/down" android:layout_toleftof="@+id/down" android:src="@drawable/left" /> <imageview android:id="@+id/up" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignparenttop="true" android:layout_toleftof="@+id/right" android:src="@drawable/up" /> <imageview android:id="@+id/down" android:layout_width="50dp" android:layout_height="50dp" android:layout_below="@+id/up" android:layout_centerhorizontal="true" android:src="@drawable/down" /> <textview android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:text="go menu" /> <textview android:id="@+id/pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:text="pause" /> </relativelayout> </linearlayout>
the activity:(part of it, out clicklisteners)
public class classicactivity extends activity { classic g; imageview left; imageview right; imageview up; imageview down; textview pause; textview back; textview scoretv; linearlayout surface; linearlayout screen; linearlayout stoped; linearlayout gameover; scoredatasource sds; button no; button yes; edittext name; button continueb; button leave; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_classic); sds=new scoredatasource(this); gameover=(linearlayout) findviewbyid(r.id.gameover); screen = (linearlayout)findviewbyid(r.id.screen); surface = (linearlayout)findviewbyid(r.id.surface); g= new classic(this, surface); surface.addview(g);
no not brining class classic
becaouse realy bigone uses other classes also, uses canvas draw on view, have set snake width , walls 10(that might problem), writing havent set width, corrdinates , color (your score:x
).
here screen shot of game on phone , on emulator:
on emulator:
on phone:
if need me add more code, or data not sure suppose upload.
how can make them match?thanks help
you using pixels when drawing... should scale them current screen density
.
make them become dp.
simply find scale factor based on current screen density
.
multiply (coordinates, sizes, ...) factor:
final float scale = resources.getsystem().getdisplaymetrics().density;
[edit]
consider difference between dp , sp (used text).
the dp has constant scale transition px: dp = px * scale
while sp has scalable ratio: sp = px * scale * ratio
or sp = dp * ratio
ratio can used people need larger font sizes, example, use device more comfortably.
let's want text 25% more dp scaling (which 125%), ratio 1.25.
Comments
Post a Comment