今天正式開始進入安卓開發(fā),編寫的是刮圖片demo
內(nèi)容
ⅠMainActivity里配置
Ⅱ在xml文件中配置
具體內(nèi)容
ⅠMainActivity里配置
1.什么是Activity - 一個界面
管理一個界面從創(chuàng)建到運行到結(jié)束的整個過程 / 生命周期
配置界面 onCreate 這個方法是系統(tǒng)調(diào)用的
啟動界面 start
重新啟動 restart
暫停界面 pause
喚醒界面 resume
銷毀界面 destroy
(1)界面啟動
onCreate
onStart
onResume
(2)點擊home鍵 回到主界面
onPause
(3)通過后臺 重新運行這個程序
onRestart
onStart
onResume
(4)使用返回鍵 返回到主界面
onPause
onDestroy
具體代碼如下:
@Override
//界面啟動 展現(xiàn)
protected void onStart() {
super.onStart();
System.out.println("onStart");
}
@Override
//重新啟動一個界面
protected void onRestart() {
super.onRestart();
System.out.println("onRestart");
}
@Override
//恢復(fù)界面 調(diào)出后臺運行到前臺
protected void onResume() {
super.onResume();
System.out.println("onResume");
}
@Override
//暫停節(jié)目 界面切換
protected void onPause() {
super.onPause();
System.out.println("onPause");
}
@Override
//界面銷毀
protected void onDestroy() {
super.onDestroy();
System.out.println("onDestroy");
}
3.程序文件介紹
Ⅱ在xml文件中配置
4.在MainActivity中配置
public class MainActivity extends AppCompatActivity {
ImageView forground;
Canvas canvas;
Bitmap orgBitmap;
Paint paint;
Matrix matrix;
Bitmap copyBitmap;
@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.drawble.fr 訪問子怨怒驚嚇 drawble里面的一個文件名為fr的資源*/
orgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.p1);
//操作這張圖片 用透明色去替換某個位置的顏色
//不能操作原圖 只能copy一份
copyBitmap = Bitmap.createBitmap(orgBitmap.getWidth(), orgBitmap.getHeight(), orgBitmap.getConfig());
//創(chuàng)建一個Canvas 畫布-相當(dāng)于畫板
canvas = new Canvas(copyBitmap);
//創(chuàng)建一個畫筆
paint = new Paint();
//創(chuàng)建一個矩陣
matrix = new Matrix();
//旋轉(zhuǎn)圖片
//matrix.setRotate(90,240,400);
//平移
//matrix.setTranslate(100,0);
//翻轉(zhuǎn) set只作用一次 post作用多次
// matrix.setScale(-1f,1f);
// matrix.postTranslate(orgBitmap.getWidth(),0);
//畫一幅圖
canvas.drawBitmap(orgBitmap, matrix, paint);
//顯示圖片
forground.setImageBitmap(copyBitmap);
//給前景圖片添加touch事件
//當(dāng)有觸摸事件發(fā)生 系統(tǒng)就會將這個事件接收并回調(diào)這個事件
forground.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//獲取當(dāng)前事件
int action = motionEvent.getAction();
//判斷狀態(tài)
if (action == MotionEvent.ACTION_MOVE) {
//獲取觸摸點的坐標(biāo)
int x = (int) motionEvent.getX();
int y = (int) motionEvent.getY();
//替換xy對應(yīng)的像素
for (int i = -8; i < 8; i++) {
for (int j = -8; j < 8; j++) {
copyBitmap.setPixel(x+i, y+i, Color.TRANSPARENT);
}
}
forground.setImageBitmap(copyBitmap);
}
return true;
}
});
}
public void code() {
//通過代碼來布局界面
//1.找一個容器 xxlayout
FrameLayout container = new FrameLayout(this);
//2.設(shè)置當(dāng)前這個界面的內(nèi)容視圖為這個容器
setContentView(container);
//創(chuàng)建一個子視圖
//創(chuàng)建ImageView顯示圖片
ImageView bgImageView = new ImageView(this);
//設(shè)置屬性
bgImageView.setBackgroundColor(Color.GREEN);
//添加到容器里面
container.addView(bgImageView);
}
1.(1)控件 = 視圖(看得到的都是視圖)
基礎(chǔ)階段 學(xué)習(xí)系統(tǒng)自帶的控件
TextView EditText Button ImageView ListView
RecycleView ScrollView ViewPager ProgressBar
Switch
(2)高級階段 自定義控件
①在已有的控件基礎(chǔ)上加上自己的功能
②自己畫
(3)一個控件就是一個類的具體對象
ImageView
屬性
方法
用java代碼配置
通過添加id號可以唯一標(biāo)識某一個控件 或者 組件(容器)
設(shè)置為 android:id="@+id/fi_main"
2.ImageView控件實現(xiàn)高度寬度的匹配、導(dǎo)入圖片以及設(shè)置id號
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/p1"/>
<ImageView
android:id="@+id/iv_forground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/p2"
/>