Wednesday, March 6, 2019

Android开发笔记-ch3.2.1 Layout & ImageView


3.2 App UI coding

3.2.1 Layout & ImageView

Refer to http://developer.android.com/guide/topics/ui/declaring-layout.html, common layouts include Linear Layout, Relative Layout, Web View, Table/grid. When the content for your layout is dynamic or not pre-determined, will need to build layouts with an Adapter, by subclasses AdapterView. More SO links about Padding vs Margin, Gravity vs layout_gravity and Match_parent vs wrap_content.
For LinearLayout, with layout_weight set, may set layout_width or layout_height to “0dp” , depends on orientation=hroizontal or vertical.
For dp, px, pt, dp/dip, sp, refer to this SO, this SO, Supporting Multiple Screens, and Screen Sizes and Densities.
The difference between android:gravity and android:layout_gravity is that android:gravity positions the contents of that view (i.e. what’s inside the view), whereas android:layout_gravity positions the view with respect to its parent (i.e. what the view is contained in).
Don't use gravity/layout_gravity with a RelativeLayout. Use them for Views in LinearLayouts and FrameLayouts.
To make the ratio consistent between phone and TV, either use PercentageLayout or make the layout programmable which give more control of all detail, like below code, design based on a background size such as below 1280x720, then get display resolution and calculate the ratio, and apply the ratio to all coordination:
int bgw=1280, bgh=720; //background is 1280x720
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
float rx=(float)size.x/bgw; //ratio to the background image
float ry=(float)size.y/bgh;
this.root = getLayoutInflater().inflate(R.layout.startbackg, null);
setContentView(this.root);
RelativeLayout ad_layout = (RelativeLayout) findViewById(R.id.ad_layout);
...
vidSurface = new SurfaceView(this); //600x356, 104 to top, 28 to right
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)(600*rx), (int)(356*ry));
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.topMargin=(int)(104*ry);//from class MarginLayoutParams which is in pixel unit
params.rightMargin=(int)(28*rx);
vidSurface.setLayoutParams(params);
ad_layout.addView(vidSurface);
dp to px, refer to this SO:
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();Or
float dp = 20f;
float fpixels = metrics.density * dp;
int pixels = (int) (fpixels + 0.5f);
Or pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
LayoutParams is applicable to ImageView as well, such as changing the ImageView size, refer to this SO. As mentioned, if image view is dynamic, getLayout will fail with null-exception. In that case the correct way is:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
iv.setLayoutParams(layoutParams);
For set ImageView from a file, refer to this:
File imgFile = new  File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    if(methoed==1) {
       myImage.setImageURI(Uri.fromFile(imgFile)); //Uri imgUri=Uri.parse("file:///data/data/MYFOLDER/myimage.png");
    } else if(method==2) {
       Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
       myImage.setImageBitmap(myBitmap);
    }
}
Add permission in the manifest file: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Someone also mentioned using picasso.
For ImageView scale to screen, add this to ImageView layout: android:scaleType="fitXY"
Set ImageView background transparent, refer to SO: android:background="@android:color/transparent" or 50%: "#7F000000"
ImageView doesn't support GIF animation. Refer this, glide, Ion, webview, and a code here. Use RotateAnimation for rotate animation:
RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);
final ImageView splash = (ImageView) findViewById(R.id.splash);
splash.startAnimation(anim);
...
splash.setAnimation(null); //Later, stop the animation
For fade in/out, refer to this so.

0 Comments:

Post a Comment