Download presentation
Presentation is loading. Please wait.
Published byAlison Gray Modified over 5 years ago
1
LayoutInflater for inflating Views Practice: In-Class App
Android Topics LayoutInflater for inflating Views Practice: In-Class App
2
What is LayoutInflater?
Allows applications to create View objects in real time. LayoutInflater takes an XML layout file and instantiates a View object from it.
3
Twitter Example: A Tweet is a View object created in real time.
LayoutInflater takes an XML layout and instantiates a View object. This View object (Tweet) is added to the LinearLayout in the Activity layout.
4
protected void onCreate(Bundle savedInstanceState) { … layoutInflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE); linearLayout = (LinearLayout) (findViewById(R.id.linearlayout)); } public void addTweet(View view) { //INSTANTIATE A TEXTVIEW TO STORE A TWEET TextView aTweet; //INFLATE THE SINGLE TWEET LAYOUT aTweet = (TextView) layoutInflater.inflate(R.layout.single_tweet_layout, null); //SET THE TEXT AND PLACE THE TWEET LAYOUT INTO THE LINEARLAYOUT aTweet.setText(“Ethereum Tweet”); linearLayout.addView(aTweet); }
5
In Class Exercise: Interactive Fibonacci Flower
Fibonacci sequence: 1, 1 , 3, 5, 8, 13, 21, 34, 55, 89, 144, etc. Golden Ratio : The ratio of successive large terms is Golden Ratio is considered to be extraordinary because of its common occurrence in nature. GOAL: Build a Fibonacci flower that uses this ratio when computing the angle of a flower petal.
6
Flower Construction The Golden Ratio is used to offset the angle of each petal before it is added to the flower. The angle of rotation for each new petal is increased by 360 degrees multiplied by the Golden Ratio. Each new petal is 3% wider and 3% longer than the previous petal.
7
The main activity layout , the “canvas” for the flower, is a RelativeLayout.
This layout will be the container for each petal. Each petal will be inflated as an ImageView and added to the RelativeView.
8
App Structure The Model of the flower. The Controller.
Two images representing the petals. The main activity layout - the “canvas” for the flower. The petal layouts that will be inflated.
9
Model for each flower petal.
public class Flower { public final double GOLDEN_RATIO = ; public final double GROW_WIDTH = .03 * GOLDEN_RATIO; public final double GROW_HEIGHT = .03 * GOLDEN_RATIO; //PETAL ATTRIBUTES private double angle; private int rotate; private float scaleX; private float scaleY; private int xCenter; private int yCenter; private float degenerate; public Flower() { rotate = 0; scaleX = (float) .2; scaleY = (float) .2; degenerate = (float) 1.001; angle = 360 * GOLDEN_RATIO; } . . . Model: Flower.java Model for each flower petal.
10
Views Task 1: Build the Activity Layout. Task 2: Build the layouts for each petal. See next slide.
12
Controller: MainActivity.java
Questions: How can we keep track of each inflated flower petal? Why is this important?
13
Controller: MainActivity.java
Answers: ArrayList can be used to keep track of each inflated flower petal. Once the View is inflated, it is added to the container View and also to the ArrayList. It is important to store these inflated Petal Views so that they can be cleared. private ArrayList<ImageView> allPetals; private LayoutInflater layoutInflater; private Button pinkBtn; private Button goldBtn; private Button clearBtn; private RelativeLayout relativeLayout; Flower myFlower;
14
TIP: Locating the Center of the Screen
//USE DisplayMetrics() to locate the center coordinate of the screen. DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); //Use setters to set the center values myFlower.set_xCenter(metrics.widthPixels / ); myFlower.set_yCenter(metrics.heightPixels / );
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.