一:ScaleType
1:把小图片放入比图片大的scaletype中的八个属性
以下图片有属性分别对应的效果图
2:把大图片片放入比图片小的scaletype中的八个属性
以下图片有属性分别对应的效果图
二:进度条
1,进度分为
ProgressBar和
ProgressBar(
horizontal
)
前者是圆形进度条后者是水平进度条
今天我来就举个例子是用水平进度条
这是在xml文件中写的代码
<ProgressBar
android:id="@+id/pb_progressBar_1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="download" android:text="下载" />这是在Java文件中写的代码
package com.example.g160828_android6_2_widget; import android.content.Intent; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends AppCompatActivity { private ProgressBar pb_progressBar_1; private TextView tv_main_1; private ImageView itv_main_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //得到进度条的id pb_progressBar_1 = (ProgressBar) findViewById(R.id.pb_progressBar_1); //得到显示数字的ID tv_main_1 = (TextView) findViewById(R.id.tv_main_1); } //进度条 public void download(View view) { new MyThread().start(); } //子程序与主线之间的中间者 Handler handler = new Handler() { //接收消息 @Override public void handleMessage(Message msg) { super.handleMessage(msg); int i = msg.what; tv_main_1.setText(i + ""); } }; //子程序 class MyThread extends Thread { @Override public void run() { super.run(); for (int i = 0; i <= 100; i++) { pb_progressBar_1.setProgress(i); //发消息 handler.sendEmptyMessage(i);try { Random r=new Random(); int n= r.nextInt(200); Thread.sleep(n); } catch (InterruptedException e) { e.printStackTrace(); }} } }}
三:Switch滑动开关
这是在xml文件中写的代码
<Switch android:id="@+id/sw_switch1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="你点不点!" android:onClick="switch1" />这是在Java文件中写的代码
public void switch1(View view) { boolean b = ((Switch) view).isChecked(); if (b) { itv_main_1.setImageResource(R.drawable.mei_1); Toast.makeText(this, " 你已经开启了哦", Toast.LENGTH_SHORT).show(); } else { itv_main_1.setImageResource(R.drawable.mei_2); Toast.makeText(this, " 你已经关闭了哦", Toast.LENGTH_SHORT).show(); } }四:seekBar这是在xml文件中写的代码
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv_seekbar_1" android:src="@drawable/mei_1" /> <SeekBar android:id="@+id/sk_seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" />这是在Java文件中写的代码
package com.example.g160828_android6_2_widget; import android.app.Activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; import android.widget.RatingBar; import android.widget.SeekBar; import android.widget.Toast; public class SeekbarActivity extends AppCompatActivity { //定义一个数组 private int image[]={R.drawable.mei_1,R.drawable.mei_2}; private int imagetext=0; private ImageView iv_seekbar_1; private SeekBar sk_seekBar; private int oop; private RatingBar ratingBar2; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_seekbar); //设置默认 //获得控件的ID iv_seekbar_1 = (ImageView) findViewById(R.id.iv_seekbar_1); sk_seekBar = (SeekBar) findViewById(R.id.sk_seekBar); ratingBar2 = (RatingBar) findViewById(R.id.ratingBar2); //设置最大值 sk_seekBar.setMax(255); //放到最大点 sk_seekBar.setProgress(255); //放一个监听 sk_seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { oop=i; } @Override public void onStartTrackingTouch(SeekBar seekBar) { iv_seekbar_1.setImageAlpha(oop); } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } public void select1(View view) { iv_seekbar_1.setImageResource(image[0]); } public void select2(View view) { iv_seekbar_1.setImageResource(image[1]); } }五:RatingBar 五星好评xml中<RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/rg_main_rg">Java代码ratingBar2.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float v, boolean b) { Toast.makeText(SeekbarActivity.this, v+"颗星星", Toast.LENGTH_SHORT).show(); } });