|
Приложение к лабораторной работеБайланысты: Силлабус Android krmu 20 Приложение к лабораторной работе
Данное приложение содержит тексты программ, рассматриваемых в примерах.
Button Example
res/layout/activity_main.xml
package="com.example.application"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="15" />
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
android:name=".MainActivity"
android:label="@string/title_activity_main" >
src/MainActivity.java
package com.example.application;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button switchToGreen;
private Button switchToRed;
private Button switchToBlue;
private LinearLayout screenLayout;
private Toast informationToast;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// init buttons
switchToBlue = (Button) findViewById(R.id.switchBlue);
switchToGreen = (Button) findViewById(R.id.switchGreen);
switchToRed = (Button) findViewById(R.id.switchRed);
screenLayout = (LinearLayout) findViewById(R.id.screenLayout);
// setup listeners
switchToBlue.setOnClickListener(this);
switchToRed.setOnClickListener(this);
switchToGreen.setOnClickListener(this);
informationToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
}
public void onClick(View view) {
if (switchToBlue.equals(view)) {
screenLayout.setBackgroundColor(Color.BLUE);
showToast("Hello blue");
} else if (switchToRed.equals(view)) {
screenLayout.setBackgroundColor(Color.RED);
showToast("Hello red");
} else if (switchToGreen.equals(view)) {
screenLayout.setBackgroundColor(Color.GREEN);
showToast("Hello green");
}
}
private void showToast(String text) {
informationToast.cancel();
informationToast.setText(text);
informationToast.show();
}
}
Animation Example
res/anim/frame_anim.xml
android:oneshot="false" >
android:drawable="@drawable/ic_launcher"
android:duration="200"/>
android:drawable="@drawable/ic_launcher1"
android:duration="200"/>
android:drawable="@drawable/ic_launcher2"
android:duration="200"/>
android:drawable="@drawable/ic_launcher3"
android:duration="200"/>
res/anim/transform_anim.xml
android:shareInterpolator="false" >
android:duration="700"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.4"
android:toYScale="0.6" />
android:duration="400"
android:fillBefore="false"
android:fromXScale="1.4"
android:fromYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:toXScale="0.0"
android:toYScale="0.0" />
android:duration="400"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:toDegrees="-45"
android:toYScale="0.0" />
src/MainActivity.java
package com.example.application;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private Button startFrameAnim;
private Button startTransformAnim;
private Button cancelAnim;
private ImageView animationView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startFrameAnim = (Button) findViewById(R.id.frameAnimationStart);
startTransformAnim= (Button) findViewById(R.id.transformAnimationStart);
cancelAnim = (Button) findViewById(R.id.cancelAnimation);
animationView = (ImageView) findViewById(R.id.animationView);
startFrameAnim.setOnClickListener(this);
startTransformAnim.setOnClickListener(this);
cancelAnim.setOnClickListener(this);
}
public void onClick(View v) {
if (startFrameAnim.equals(v)) {
animationView.setBackgroundResource(R.anim.frame_anim);
AnimationDrawable animation =
(AnimationDrawable) animationView.getBackground();
animation.start();
} else if (startTransformAnim.equals(v)) {
animationView.setBackgroundResource(R.drawable.ic_launcher);
Animation transformAnimation =
AnimationUtils.loadAnimation(this, R.anim.transform_anim);
animationView.startAnimation(transformAnimation);
} else if (cancelAnim.equals(v)) {
animationView.setBackgroundColor(Color.BLACK);
}
}
}
Location Example
src/MainActivity.java
package com.example.application;
import java.util.Date;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity implements LocationListener {
private TextView latitudeLabel;
private TextView longitudeLabel;
private TextView statusLabel;
private LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeLabel = (TextView) findViewById(R.id.latitudeLabel);
longitudeLabel = (TextView) findViewById(R.id.longitudeLabel);
statusLabel = (TextView) findViewById(R.id.statusLabel);
locationManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
// construct a criteria with best accuracy
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// get best ENABLED provider that meets the criteria
String provider = locationManager.getBestProvider(criteria, true);
// request the updates
locationManager.requestLocationUpdates(provider, 0, 0, this);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
public void onLocationChanged(Location location) {
statusLabel.setText("Location recieved at " + new Date());
latitudeLabel.setText("Latitude: " + location.getLatitude());
longitudeLabel.setText("Longitude: " + location.getLongitude());
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
AndroidManifest.xml
package="com.example.application"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="15" />
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
android:name=".MainActivity"
android:label="@string/title_activity_main" >
Достарыңызбен бөлісу: |
|
|