一绽昏、學習內(nèi)容
1.Activity的生命周期
ndroid里面大部分的控件都會提供很多方法
這些方法只需要自己去重寫 不需要自己主動調(diào)用
什么是Activity = 就是一個界面
管理一個界面從創(chuàng)建到運行到結束的整個過程 / 生命周期
配置界面 onCreate 這個方法是系統(tǒng)調(diào)用的
啟動界面 start
重新啟動 restart
喚醒界面 resume
暫停界面 pause
銷毀界面 destory
2.對應接口的調(diào)用順序
界面啟動
onCreate
onStart
onResume點擊home鍵 回到主頁面
onPause通過后臺 重新運行這個程序
onRestart
onStart
onResume使用返回鍵 返回到主界面
onPause
onDestory
3.xml界面布局
使用兩種方式界面布局
- 1.xml配置
- 2.使用Java代碼創(chuàng)建
默認一個Activity對應一個xml配置文件
命名特點:activity_界面功能.xml
xml文件就是一個容器:可以放很多UI控件
布局:這么多控件該如何布局
- 1.約束布局 ConstraintLayout
- 2.線性布局 LinearLayout 垂直 水平
- 3.相對布局 RelativeLayout
- 4.幀布局 FrameLayout
- 5.表格布局 TableLayout GridLayout
- 6.絕對布局 AbsoluteLayout
4.xml添加控件
控件的尺寸
父視圖:
子視圖:
將一個控件添加到一個容器中默勾,控件就是這個容器的子視圖
容器就是這個控件的父視圖
- 1.match_parent 和父視圖一樣大
- 2.wrap_content 包裹內(nèi)容 和控件的內(nèi)容一樣大
- 3.20dp 具體尺寸
二、技術具體實現(xiàn)
1.xml添加控件
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bg" />
<ImageView
android:id="@+id/iv_forground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/fr"/>
2.代碼方式創(chuàng)建控件
public void code(){
//通過代碼來布局界面
//1.找一個容器 xxlayout
FrameLayout container= new FrameLayout(this);
//3.創(chuàng)建一個子視圖
//創(chuàng)建ImageView顯示圖片
ImageView bgImageView=new ImageView(this);
//設置屬性
bgImageView.setBackgroundColor(Color.GREEN);
//添加到容器里面
container.addView(bgImageView,600,800);
//2.設置當前這個界面的內(nèi)容視圖為這個容器
setContentView(container);
}
3.配置界面
//配置界面
setContentView(R.layout.activity_main);
//找到容器里面的圖片視圖控件
//findViewById
forground=findViewById(R.id.iv_forground);
4.讀取原圖
//將需要操作的圖片讀取出來 Bitmap
//BitmapFactroy 用于管理位圖
//decodeResource 從工程的資源路徑中去生成一張位圖
//getResources()獲取工程的資源
//R.drawable.fr 訪問資源路徑下 drawable里面的一個
orgBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.fr);
5.創(chuàng)建副本
//操作這張圖片 用透明色去替換某個位置的顏色
//不能操作原圖 只能copy一份
//創(chuàng)建一個和原始圖片相同環(huán)境的空位圖
copyBitmap=Bitmap.createBitmap(orgBitmap.getWidth(),orgBitmap.getHeight(),orgBitmap.getConfig());
6.圖片操作
//創(chuàng)建一個Canvas 畫布-現(xiàn)實中的畫板
canvas=new Canvas(copyBitmap);
//創(chuàng)建一個畫筆
paint=new Paint();
//創(chuàng)建一個矩陣
Matrix matrix=new Matrix();
//旋轉圖片
//matrix.setRotate(90,240,400);
//平移
//matrix.setTranslate(100,0);
//翻轉 set只作用一次 post作用多次
//matrix.setScale(-1f,1f);
//matrix.postTranslate(orgBitmap.getWidth(),0);
//畫一幅圖
canvas.drawBitmap(orgBitmap,matrix,paint);
//顯示圖片
forground.setImageBitmap(copyBitmap);
7.添加觸摸事件
forground.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//獲取當前事件
int action=motionEvent.getAction();
//判斷狀態(tài)
if (action==MotionEvent.ACTION_DOWN){
//獲取觸摸點的坐標
int x=(int) motionEvent.getX();
int y=(int)motionEvent.getY();
//替換x,y對應的像素
for (int i = -8; i <8; i++) {
for (int j = -8; j <8 ; j++) {
copyBitmap.setPixel(x+i,y+j,Color.TRANSPARENT);
}
}
//canvas.drawBitmap(orgBitmap,new Matrix(),paint);
forground.setImageBitmap(copyBitmap);
}
return true;
}
});