0% found this document useful (0 votes)
6 views9 pages

Android Graphics and GPS Applications

Uploaded by

manasishivarkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Android Graphics and GPS Applications

Uploaded by

manasishivarkar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical No.

03

Write an application that draws basic graphical primitives on the screen.

Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />
</RelativeLayout>

[Link]:
package [Link].exno3;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
[Link](savedInstanceState);
setContentView([Link].activity_main);
//Creating a Bitmap
Bitmap bg = [Link](720, 1280, [Link].ARGB_8888);
//Setting the Bitmap as background for the ImageView
ImageView i = (ImageView) findViewById([Link]);
[Link](new BitmapDrawable(bg));
//Creating the Canvas Object
Canvas canvas = new Canvas(bg);
//Creating the Paint Object and set its color & TextSize
Paint paint = new Paint();
[Link]([Link]);
[Link](50);
//To draw a Rectangle
[Link]("Rectangle", 420, 150, paint);
[Link](400, 200, 650, 700, paint);
//To draw a Circle
[Link]("Circle", 120, 150, paint);
[Link](200, 350, 150, paint);
//To draw a Square
[Link]("Square", 120, 800, paint);
[Link](50, 850, 350, 1150, paint);
//To draw a Line [Link]("Line", 480, 800, paint);
[Link](520, 850, 520, 1150, paint);
}}
Practical No. 04

Develop a native application that uses GPS location information.

[Link]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
android:id="@+id/relativeLayout1"
android:layout width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/show_Location"
android:layout width="wrap_content"
android:layout height="wrap_content"
android:text="Show_Location"
android:layout_centerVertical="true"
android:layout centerHorizontal="true"/>
</RelativeLayout>

[Link]
package [Link];
import [Link];
import [Link];
import [Link]; import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link]; import [Link];
import [Link];
public class GPStrace extends Service implements LocationListener{
private final Context context;
boolean isGPSEnabled=false;
boolean canGetLocation-false;
boolean isNetworkEnabled=false;
Location location:
double latitude:
double longtitude;
private static final long
MIN DISTANCE CHANGE FOR UPDATES-10;
private static final long MIN_TIME_BW_UPDATES-1000*60*1;
protected LocationManager locationManager:
public GPStrace (Context context)
{
[Link]=context; getLocation();
}
public Location getLocation()
{
try{ locationManager=(LocationManager) [Link](LOCATION_SERVICE);
isGPSEnabled=[Link] (Location [Link] PROVIDER);
isNetworkEnabled=[Link] (Locat [Link] PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) (
}else{
[Link]=true;
if(isNetworkEnabled) { [Link] ( [Link]
PROVIDER, MIN
TIME BW UPDATES,
MIN DISTANCE CHANGE FOR UPDATES,this);
if(locationManager!=null){
location=[Link] KnownLocation (LocationMa [Link] PROVIDER);
if(location!=null)(
latitude=[Link]();
[Link]();
if(isGPSEnabled) {
if(location==null){
[Link] (LocationManager.G
PS_PROVIDER, MIN_TIME_BW_UPDATES, MIN DISTANCE CHANGE FOR UPDATES, this);
if (locationManager!=null){
location=[Link] (LocationMa nager.GPS_PROVIDER);
if (location!=null) {
[Link]();
longtitude=[Link]();
}
catch (Exception e)
[Link]();
return location;
}
public void stopUsingGPS () {
if (locationManager!=null) {
[Link] ([Link]);
}
public double getLatitude () {
if (location!=null){
latitude=[Link]();
return latitude;
public double getLongtiude(){
if (location!=null) (
longtitude=[Link]();
} return longtitude;
public boolean canGetLocation()(
return [Link]:
public void showSettingAlert(){
[Link] alertDialog=new [Link](context);
[Link]("GPS is settings");
[Link]("GPS is not enabled. Do you want to go to setting menu?");
[Link]("settings", new
[Link]() {
@Override
public void onClick(DialogInterface dialog, int which) (Intent intent-new Intent ([Link]
LOCATION SOURCE SETTINGS); [Link](intent);
}});
[Link]("cancel", new [Link]() {
@Override
public void onClick(DialogInterface dialog, int
which) {
[Link]();
@Override
public void onLocationChanged (Location location) {
@Override
public void onProviderDisabled(String provider)
}
@Override
public void onProviderEnabled(String provider) {
@Override
public void onStatusChanged (String provider, int status,
Bundle extras) f
@override
public Binder onBind(Intent intent)
// TODO Auto-generated method stub
return null
}
}
Practical No. 05

Design an android application for frame animation

animation_list.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="[Link] android:oneshot="false">
<item android:drawable="@drawable/blank" android:duration="210" />
<item android:drawable="@drawable/logo" android:duration="210" />
<item android:drawable="@drawable/logo1" android:duration="210" />
<item android:drawable="@drawable/logo2" android:duration="210" />
<item android:drawable="@drawable/logo3" android:duration="210" />
<item android:drawable="@drawable/logo4" android:duration="210" />
<item android:drawable="@drawable/logo5" android:duration="210" />
<item android:drawable="@drawable/logo6" android:duration="210" />
<item android:drawable="@drawable/logofinal" android:duration="210" />
</animation-list>

activity_main.xml
<span style="font-size: 13px; text-align: center;">Here we are done with the xml part, see the image below
for reference :-
</span>
<p style="text-align: center;"><a href="<a href="[Link]
inandroid/">[Link] target="_blank"><img
class="aligncenter sizefull wp-image-2382" title="XML part in Frame animation" alt="XML part in Frame
animation" src="<a
href="[Link]
[Link]/blog/wpcontent/uploads/2013/02/[Link]</a>" width="845" height="603"
/></a></p>
<strong> </strong>
<h2><span style="font-size: large;"><strong>

Outside the onCreate method :</strong></span></h2>


<strong></strong>

Declare the Image View and Animation Drawable


[code lang="java"]
// Declaring an Image View and an Animation Drawable
ImageView view;
AnimationDrawable frameAnimation;

Step 3- Inside the OnCreate method:


• Typecast the Image view
• Typecast the Animation Drawable
• Set the drawable background on the image view
// Typecasting the Image View
view = (ImageView) findViewById([Link]);
// Setting animation_list.xml as the background of the image view
[Link]([Link].animation_list);
// Typecasting the Animation Drawable
frameAnimation = (AnimationDrawable) [Link]();

Step 4- After the onCreate method :


The animation should only run when it is in focus that is when it is visible to the user. Hence define this
method after the
onCreate method.
// Called when Activity becomes visible or invisible to the user
@Override
public void onWindowFocusChanged(boolean hasFocus) {
[Link](hasFocus);
if (hasFocus) {
// Starting the animation when in Focus
[Link]();
} else {
// Stoping the animation when not in Focus
[Link]();
}
}

Common questions

Powered by AI

The GPStrace.java class implements the LocationListener interface, thereby allowing it to handle changes in location provider status using overridden methods like onProviderEnabled and onProviderDisabled. These methods facilitate the tracking of when a provider becomes available or unavailable, respectively, although the specific operational changes in response to these events aren't detailed in the document. This setup helps in managing provider changes effectively .

GPStrace.java employs a strategy where network-based locations are requested first if the network provider is enabled. The code checks network provider status and requests location updates if active, using getLastKnownLocation to obtain the latest position. GPS-based updates are only requested if the GPS provider is enabled and the network location is unavailable, functioning as a fallback. This conditional configuration capitalizes on potentially faster network updates while preserving the accuracy benefits of GPS when necessary. The service effectively switches to GPS when finer location results are required or the network is insufficient .

The canGetLocation method in GPStrace.java returns a boolean indicating if location retrieval is possible, determined during the setup phase of the LocationManager. If either GPS or network providers are enabled, canGetLocation is set to true. This method essentially serves as a gatekeeping mechanism to check if the application has the capability to access location services, influencing subsequent logic on whether to attempt to obtain location updates .

In the frame animation example, AnimationDrawable is used to run a sequence of images defined in animation_list.xml. It's typecast from the ImageView's background and managed to start or stop based on window focus. The key lifecycle consideration is its resource management, ensuring the animation only runs when the window is in focus through onWindowFocusChanged, preventing unnecessary resource usage when the Activity is not visible. This attention to lifecycle and resource efficiency is crucial for optimal app performance .

In MainActivity.java, each shape is drawn at a specific position on the canvas using methods like drawRect and drawCircle with coordinate parameters that define their positions. For instance, the rectangle is drawn with coordinates (400, 200, 650, 700) and the circle is centered at (200, 350) with a radius of 150. Text labels are drawn with the drawText method, specifying the string (e.g., "Rectangle"), as well as x and y coordinates to position the text near the associated shapes. This setup uses a coordinated approach to align labels with their respective shapes based on chosen canvas positions .

The GPStrace.java file manages GPS location updates by implementing the LocationListener interface and utilizing a LocationManager to request location updates from both GPS and network providers. It sets a minimum distance change of 10 meters and a minimum time interval of 1 minute for receiving updates, controlled by constants. getLocation() checks if either GPS or network providers are enabled, requests updates from enabled providers, and retrieves the last known location if available. The onLocationChanged method is implemented as part of managing changes in location .

The MainActivity.java file in the application creates a Bitmap and sets it as the background for an ImageView. It uses a Canvas object to draw various basic graphical shapes. The code first constructs a Paint object, setting its color to blue and text size to 50. It draws the shapes and their names on the canvas using methods like drawText(), drawRect(), and drawCircle(). For example, to draw a rectangle, the code uses drawRect() with specified coordinates, and similarly for the circle and square, different methods and dimensions are used .

MainActivity.java sets up its view and UI components through programmatic methods after calling setContentView with the layout resource activity_main. It initializes an ImageView by finding it via its ID using findViewById and setting a Bitmap as its background using setBackgroundDrawable. This programmatic setup allows for manual and dynamic control over UI elements and their properties, such as dimensions and content, directly from the Java code .

The GPStrace service checks if the GPS is enabled by using the LocationManager's isProviderEnabled method, checking both GPS and network providers. If neither provider is enabled, it proceeds to alert the user through the showSettingAlert method. This method constructs an AlertDialog, notifying users with a message that GPS is not enabled, and offers an option to go to the settings through an intent with Settings.ACTION_LOCATION_SOURCE_SETTINGS. This direct interaction enhances user awareness and provides a mechanism for enabling location services .

Frame animation in the Android application is configured using the animation_list.xml file, which specifies a sequence of drawable resources (images) with each assigned a duration of 210 milliseconds. These resources represent different frames for the animation. The Activity code begins by setting animation_list.xml as the background of an ImageView, and typecasting it to an AnimationDrawable. The animation management is done by overriding the onWindowFocusChanged method, which starts the animation when the Activity is in focus and stops it when it is not. This ensures the animation only runs when visible to the user, efficiently managing resources .

You might also like