學習內(nèi)容
part 1.
程序目錄介紹part 2.
Activity就是一個界面
管理一個界面蟲創(chuàng)建到運行到結(jié)束的整個過程/生命周期
配置界面 onCreate 這個方法是系統(tǒng)調(diào)用的
啟動界面 start
重新啟動 restart
喚醒界面 resume
暫停界面 pause
銷毀界面 destroy
界面啟動
onCreate
onStart
onResume點擊home鍵 回到主界面
onPause通過后臺重新運行這個程序
onRestart
onStart
onResume使用返回鍵返回到主界面
onPause
onDestroy
part 3.
- 使用兩種方式界面布局:
1.xml配置
2.使用java代碼配置 - 使用java代碼來布局界面
通過添加id號可以唯一標識一個控件或組件(容器)
android:id="@+id/fl-main"
- xml:解耦 (安卓推薦使用
什么時候需要用代碼創(chuàng)建 什么時候用xml配置
如果添加的組件是靜態(tài)的 (變化不多的東西)選擇xml配置
如果需要靈活地操作這個控件,選擇代碼創(chuàng)建
默認一個Activity對應(yīng)一個xml配置文件
命名特點:activity_界面功能.xml
xml文件就是一個容器:可以放很多UI控件
- 布局:這么控件該如何布局
1.約束布局:ConstraintLayout
2.線性布局:LinearLayout 垂直 水平
3.相對布局:RelativeLayout
4.幀布局:FrameLayout
5.表格布局:TableLayout GridLayout
6.絕對布局:AbsoluteLayout
Android 幾種常用布局詳解:http://www.reibang.com/p/2598626b1b04
part 4.
- 控件
一個控件就是一個類的具體對象
ImageView
屬性
方法
控件=一個視圖=看到的都是控件
1.學習系統(tǒng)自帶的控件 熟悉
TextView EditText Button ImageView ListView RecycleView ScrollView ViewPager ProgressBar Switch
2.高級階段:自定義
①在已有的控件基礎(chǔ)上加上自己的功能(繼承)
②自己畫 - 控件的尺寸
父視圖:
子視圖:
將一個控件添加到一個容器中,控件就是這個容器的子視圖独郎,容器就是這個控件的父視圖
1.match_parent 和父視圖一樣大
2.wrap_content 包裹內(nèi)容和控件的內(nèi)容一樣大
3.20dp 具體尺寸
實際操作
- 思路:
使用透明色去替換原有圖片對應(yīng)的像素,立刻獲取替換之后的圖片 將圖片顯示在ImageView上
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/fl.main">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/download"/>
<ImageView
android:id="@+id/iv_forground"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
ImageView forground;
Bitmap copyBitmap;
Canvas canvas;
Paint paint;
Bitmap orgBitmap;
@Override//創(chuàng)建一個界面 界面如何布局
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//配置界面
setContentView(R.layout.activity_main);
//找到容器里面的圖片視圖控件
//findViewById
forground = findViewById(R.id.iv_forground);
//將需要操作的圖片讀取出來 Bitmap
//BitmapFactory 用于管理位圖
//decodeResource 從工程的資源中去生成一張位圖
//getResources()獲取工程的資源
//R.drawable.fr訪問資源路徑下 drawable里面的一個文件名為download的資源
orgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.download2);
//操作這張圖片 用透明色去替換某個位置的顏色
//不能操作原圖 只能copy一份
//創(chuàng)建一個和原始圖片相同環(huán)境的空位圖
copyBitmap = Bitmap.createBitmap(orgBitmap.getWidth(),
orgBitmap.getHeight(), orgBitmap.getConfig());
//創(chuàng)建一個Canvas 畫布-現(xiàn)實中的畫板
canvas = new Canvas(copyBitmap);
//創(chuàng)建一個畫筆
paint = new Paint();
//創(chuàng)建一個矩陣
Matrix matrix = new Matrix();
//旋轉(zhuǎn)圖片
//matrix.setRotate(90,100,200);
//平移
//matrix.setTranslate(50,0);
//翻轉(zhuǎn)
// matrix.setScale(-1f,1f);
//
//畫一幅圖
canvas.drawBitmap(orgBitmap, matrix, paint);
//顯示圖片
forground.setImageBitmap(copyBitmap);
//給前景圖片添加touch事件
//當有觸摸事件發(fā)生 系統(tǒng)就會監(jiān)聽這個事件接收并回調(diào)這個事件
forground.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//獲取當前事件
int action = event.getAction();
//判斷狀態(tài)
if (action == MotionEvent.ACTION_MOVE) {
//獲取觸摸點的坐標
int x = (int) event.getX();
int y = (int) event.getY();
System.out.println(canvas);
System.out.println("x:" + x + "y:" + y);
//替換對應(yīng)的x y像素
for (int i = -50; i < 10; i++) {
for (int j = -50; j <10; j++) {
copyBitmap.setPixel(x+i, y+j, Color.TRANSPARENT);
}
}
//copyBitmap.setPixel(x,y,Color.TRANSPARENT);
//canvas.drawBitmap(orgBitmap,new Matrix(),paint);
forground.setImageBitmap(copyBitmap);
}
return true;
}
});
}
}
style.xml
程序或者某個UI模塊都可以有自己的樣式Style肉瓦,可以在values.style.xml里面定義棒掠,不需要ActionBar
parent="Theme.AppCompat.Light.NoActionBar"
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
最終效果缺點:鼠標的位置和實際畫的位置相差太大