When I was studying the homework written by Android application development, I was going to delete the document and put it here as a souvenir (?).
①ImageView
activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView1"
android:padding="2dp"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
This code snippet implements a simple Android layout that includes an ImageView control that displays an image with a black background in the center of the screen.
②CheckBox
activity_main.xml
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Make the button available" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="start"
android:layout_below="@+id/checkBox1"
android:layout_alignParentStart="true"
android:layout_alignEnd="@+id/checkBox1" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"The button was clicked!",Toast.LENGTH_SHORT).show();
}
}
);
CheckBox checkbox=(CheckBox) findViewById(R.id.checkBox1);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
if (isChecked){
button.setEnabled(true);
}
else{
button.setEnabled(false);
}
}
});
}
}
demens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="activity_horizontal_margin">16dp</dimen>
</resources>
This code implements a simple Android application that contains a CheckBox and a Button control.
The CheckBox control is used to control the available state of the Button control. Even if the Button control is initially unavailable, when the CheckBox control is checked, the Button control will become available.
①ProgressBar
Activity_main.xml
<TextView
This code implements an Android application that includes GridView and ImageSwitcher controls.
The GridView control displays several pictures with a caption below each picture. When the user clicks on one of the pictures, the picture is displayed in the ImageSwitcher control.
Aactivity_main.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Calculate your horoscope"
android:padding="20dp"/>
<LinearLayout
android:id="@+id/linearLayout1"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:text="Gregorian birthday:"/>
<EditText
android:id="@+id/birthday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:hint="Please enter date of birth (YYYY-MM-DD)" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:text="Format: YYYY-MM-DD For example: 2012-12-31"/>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"/>
</LinearLayout>
ResultActivity.java
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.result);//Set the layout used by the Activity
/ / Get the text box that displays the birthday
TextView birthday = (TextView) findViewById(R.id.birthday);
/ / Get the text box displaying the constellation
TextView result = (TextView) findViewById(R.id.result);
Intent intent = getIntent();//Get the Intent object
Bundle bundle = intent.getExtras();//Get the passed data package
//Get a serializable Info object
Info info = (Info) bundle.getSerializable("info");
//Get the gender and display it in the corresponding text box
birthday.setText("Your Gregorian birthday is" + info.getBirthday());
result.setText( query(info.getBirthday()));//Display the calculated constellation
}
public String query(String birthday) {
int month = 0;
int day = 0;
try {
month = Integer.parseInt(birthday.substring(5, 7));
day = Integer.parseInt(birthday.substring(8, 10));
} catch (Exception e) {
e.printStackTrace();
}
String name = "";//prompt information
if (month > 0 && month < 13 && day > 0 && day < 32) {
if ((month == 3 && day > 20) || (month == 4 && day < 21)) {
name = "You are Aries!";
} else if ((month == 4 && day > 20) || (month == 5 && day < 21)) {
name = "You are a Taurus!";
} else if ((month == 5 && day > 20) || (month == 6 && day < 21)) {
name = "You are a Gemini!";
} else if ((month == 6 && day > 21) || (month == 7 && day < 21)) {
name = "You are Cancer!";
} else if ((month == 7 && day > 22) || (month == 8 && day < 21)) {
name = "You are a Leo!";
} else if ((month == 8 && day > 22) || (month == 9 && day < 21)) {
name = "You are a Virgo!";
} else if ((month == 9 && day > 22) || (month == 10 && day < 21)) {
name = "You are Libra!";
} else if ((month == 10 && day > 22) || (month == 11 && day < 21)) {
name = "You are a Scorpio!";
} else if ((month == 11 && day > 21) || (month == 12 && day < 21)) {
name = "You are a Sagittarius!";
} else if ((month == 12 && day > 21) || (month == 1 && day < 21)) {
name = "You are a Capricorn!";
} else if ((month == 1 && day > 19) || (month == 2 && day < 21)) {
name = "You are a Taurus!";
} else if ((month == 2 && day > 18) || (month == 2 && day < 21)) {
name = "You are a Pisces!";
}
name = month + "月" + day + "日" + name;
} else {
name = "The birthday you entered is in an incorrect format or is not a real birthday!";
}
return name;
}
}
Result.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/birthday"
android:padding="10px"
android:text="Gregorian birthday" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/result"
android:padding="10px"
android:text="constellation" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Info info=new Info();
if("".equals(((EditText)findViewById(R.id.birthday)).getText().toString())){
Toast.makeText(MainActivity.this,"Please enter your Gregorian birthday, otherwise it cannot be calculated!",Toast.LENGTH_SHORT).show();
return;
}
String birthday=((EditText)findViewById(R.id.birthday)).getText().toString();
info.setBirthday(birthday);
Bundle bundle=new Bundle();
bundle.putSerializable("info",info);
Intent intent=new Intent(MainActivity.this,ResultActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
Info.java
public class Info implements Serializable {
private static final long seriaVersionUID=1L;
private String birthday="";//birthday
public String getBirthday(){
return birthday;
}
public void setBirthday(String birthday){
this.birthday=birthday;
}
}
This is an Android app for calculating horoscopes. Users need to enter their birthday in the Gregorian calendar on the page. After clicking the "OK" button, it will jump to another page to display the user's horoscope.
① Searchlight effect
Aactivity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</FrameLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the frame layout manager in the layout file
FrameLayout ll=(FrameLayout) findViewById(R.id.frameLayout1);
ll.addView(new MyView(this));//Add the defined view to the frame layout manager
}
public class MyView extends View {
private Bitmap bitmap;//source image, also background image
private ShapeDrawable drawable;
private final int RADIUS=200;//searchlight radius
private Matrix matrix=new Matrix();
public MyView(Context context){
super(context);
Bitmap bitmap_source= BitmapFactory.decodeResource(getResources(),
R.drawable.source);//Get the source picture to be displayed
bitmap=bitmap_source;
BitmapShader shader=new BitmapShader(Bitmap.createScaledBitmap(
bitmap_source,bitmap_source.getWidth(),
bitmap_source.getHeight(),true), Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);//Create BitmapShader object
// circle drawable parameters
drawable=new ShapeDrawable(new OvalShape());
drawable.getPaint().setShader(shader);
drawable.setBounds(0,0,RADIUS*2,RADIUS*2);//Set the circumscribed rectangle of the circle
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint p=new Paint();
p.setAlpha(50);
canvas.drawBitmap(bitmap,0,0,p);//Draw the background picture
drawable.draw(canvas);//draw the picture illuminated by the searchlight
}
@Override
public boolean onTouchEvent(MotionEvent event){
final int x=(int) event.getX();//Get the X-axis coordinate of the current contact point
final int y=(int) event.getY();//Get the Y-axis coordinate of the current touch point
//Translate to the starting position of drawing the shadow
matrix.setTranslate(RADIUS-x,RADIUS-y);
drawable.getPaint().getShader().setLocalMatrix(matrix);
//Set the circumscribed rectangle of the circle
drawable.setBounds(x-RADIUS,y-RADIUS,x+RADIUS,y+RADIUS);
invalidate();//Redraw the canvas
return true;
}
}
}
This is an Android app that realizes the searchlight effect. Users can touch the screen, and when moving on the screen, a circle of light will be displayed to simulate the effect of a searchlight.
② Realize the video player
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG="VideoView";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//no title
requestWindowFeature(Window.FEATURE_NO_TITLE);
// set to full screen mode
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Force to landscape mode
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_main);
final VideoView videoView=(VideoView) findViewById(R.id.VideoView01);
Button pauseButton = findViewById(R.id.PauseButton);
Button loadButton = findViewById(R.id.LoadButton);
Button playButton = findViewById(R.id.PlayButton);
loadButton.setOnClickListener(v -> {
videoView.setVideoPath("/sdcard/apple.mp4");
videoView.setMediaController(new MediaController(MainActivity.this));
videoView.requestFocus();
});
playButton.setOnClickListener(v -> {
Log.v(TAG,"start");
videoView.start();
Log.v(TAG,"start OK");
});
pauseButton.setOnClickListener(v -> {
videoView.pause();
});
}
}
activity_main.xml
<VideoView
android:id="@+id/VideoView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/LoadButton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/LoadButton"
android:text="loading"
android:layout_x="30px"
android:layout_y="300px"
android:textSize="40dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/PlayButton"
android:text="play"
android:layout_x="120px"
android:layout_y="300px"
android:textSize="40dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/LoadButton"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/PauseButton"
android:text="Pause"
android:layout_x="210px"
android:layout_y="300px"
android:textSize="40dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/PlayButton"/>
This is an Android video player implementation that can play video files stored on the device.
App Demo Video
This App can realize Bluetooth sending message, NFC data transmission, GPS positioning and short message function. The following details are as follows:
- Bluetooth send message function
This feature allows users to connect to and send messages to other devices using Bluetooth technology. In the App, users can search for surrounding Bluetooth devices after turning on this function, and select the device to be connected for pairing. When the connection is successful, the user can send messages to other devices, and also receive messages from other devices.
- NFC transfer function
This feature allows users to transfer data using Near Field Communication (NFC) technology. In the App, users only need to turn on this function, put their device close to other devices, and keep the distance between them within 4cm for data transmission.
- location function
This function allows the user to obtain the GPS positioning information of the current device and display it. In the App, users can view the current location information after turning on this function.
- Send SMS function
This feature allows users to enter a phone number and send a text message. In the App, the user only needs to enter the number that he/she wants to send a text message to, and the user can send him/her a text message.