最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android使用WindowManager制作一個可拖動的控件

 更新時間:2016年08月02日 16:35:38   投稿:lijiao  
這篇文章主要為大家詳細介紹了Android使用WindowManager制作一個可拖動的控件的相關(guān)資料,感興趣的小伙伴們可以參考一下

效果圖如下

第一步:新建DragView繼承RelativeLayout

package com.rong.activity;

import com.rong.test.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

public class DragView extends RelativeLayout {
 private WindowManager windowManager;// 用于可拖動的浮動窗口
 private WindowManager.LayoutParams windowParams;// 浮動窗口的參數(shù)
 private Button myButton;

 public DragView(Context context, AttributeSet attrs) {
 super(context, attrs);
 init();
 }

 private void init() {
 View.inflate(getContext(), R.layout.layout_my, this);
 myButton = new Button(getContext());
 myButton.setText("我的");
 myButton.setBackgroundColor(Color.RED);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
 // 獲取當(dāng)前點的xy位置
 int currentX = (int) event.getX();
 int currentY = (int) event.getY();
 switch (event.getAction()) {
 case MotionEvent.ACTION_DOWN:
  if (windowManager == null) {
  setWindowParams(currentX, currentY);
  windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
  windowManager.addView(myButton, windowParams);
  }
  break;
 case MotionEvent.ACTION_MOVE:
  windowParams.x = currentX;
  windowParams.y = currentY;
  windowManager.updateViewLayout(myButton, windowParams);
  break;
 case MotionEvent.ACTION_UP:
  // windowManager.removeView(myButton);
  break;
 }
 return true;
 }

 private void setWindowParams(int x, int y) {
 // 建立item的縮略圖
 windowParams = new WindowManager.LayoutParams();
 windowParams.gravity = Gravity.TOP | Gravity.LEFT;// 這個必須加
 // 得到preview左上角相對于屏幕的坐標(biāo)
 windowParams.x = x;
 windowParams.y = y;
 // 設(shè)置寬和高
 windowParams.width = 200;
 windowParams.height = 200;
 windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
  | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
  | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
 windowParams.format = PixelFormat.TRANSLUCENT;
 windowParams.windowAnimations = 0;
 }
}

第二步:新建布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/main_touchlayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ffffff"
  android:orientation="vertical" >

  <com.rong.activity.DragView
    android:id="@+id/main_touchview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:background="#ff0000" />

</RelativeLayout>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android實現(xiàn)WebView刪除緩存的方法

    Android實現(xiàn)WebView刪除緩存的方法

    這篇文章主要介紹了Android實現(xiàn)WebView刪除緩存的方法,實例分析了Android針對WebView操作緩存的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • Android實現(xiàn)炫酷的網(wǎng)絡(luò)直播彈幕功能

    Android實現(xiàn)炫酷的網(wǎng)絡(luò)直播彈幕功能

    這篇文章主要為大家詳細介紹了Android仿網(wǎng)絡(luò)直播彈幕功能的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android中使用背景色Alpha值遇到的一個坑

    Android中使用背景色Alpha值遇到的一個坑

    通過修改Alpha值可以對透明度進行設(shè)置,這個大家應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于Android中使用背景色Alpha值遇到的一個坑,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。
    2017-12-12
  • iOS開發(fā)中TableView類似QQ分組的折疊與展開效果

    iOS開發(fā)中TableView類似QQ分組的折疊與展開效果

    這篇文章主要介紹了iOS開發(fā)中TableView類似QQ分組的折疊與展開效果,其實要做這個效果我先想到的是在tableView中再嵌套多個tableView。下面通過本文給大家分享實現(xiàn)思路,需要的朋友可以參考下
    2016-12-12
  • Android創(chuàng)建文件時出現(xiàn)java.io.IOException:?Operation?not?permitted異常的解決方法

    Android創(chuàng)建文件時出現(xiàn)java.io.IOException:?Operation?not?permitte

    最近使用android10創(chuàng)建文件失敗,并拋出權(quán)限異常,這篇文章主要給大家介紹了Android創(chuàng)建文件時出現(xiàn)java.io.IOException:?Operation?not?permitted異常的解決方法,需要的朋友可以參考下
    2023-05-05
  • Android?Jetpack組件Navigation導(dǎo)航組件的基本使用

    Android?Jetpack組件Navigation導(dǎo)航組件的基本使用

    本篇主要簡單介紹了一下?Navigation?是什么?以及使用它的流程是什么,并且結(jié)合實際案例?操作了一番,Navigation?還有很多其他用法,如條件導(dǎo)航、嵌套圖、過度動畫?等等功能?有機會再操作,需要的朋友可以參考下
    2022-06-06
  • Android studio 運行main 函數(shù)的方法

    Android studio 運行main 函數(shù)的方法

    這篇文章主要介紹了Android studio 運行main 函數(shù)的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標(biāo)簽用法示例

    Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標(biāo)簽用法示例

    這篇文章主要介紹了Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標(biāo)簽用法,實例分析了Android中TextView控件的使用技巧,需要的朋友可以參考下
    2016-07-07
  • android布局優(yōu)化的一些實用建議

    android布局優(yōu)化的一些實用建議

    這篇文章主要給大家介紹了關(guān)于android布局優(yōu)化的一些實用建議,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • 舉例講解Android中ViewPager中的PagerTitleStrip子控件

    舉例講解Android中ViewPager中的PagerTitleStrip子控件

    這篇文章主要介紹了Android中ViewPager中的PagerTitleStrip子控件使用例子,講解了PagerTitleStrip子控件的嵌入與設(shè)置標(biāo)題的用法,需要的朋友可以參考下
    2016-03-03

最新評論

石泉县| 佛坪县| 南乐县| 黑河市| 舞钢市| 白朗县| 湖北省| 昭平县| 边坝县| 承德市| 宁晋县| 宜君县| 麻江县| 株洲市| 灵丘县| 富民县| 杭锦后旗| 淮南市| 浙江省| 林芝县| 项城市| 科技| 昆明市| 海林市| 兴文县| 东辽县| 蓬安县| 都江堰市| 锡林郭勒盟| 阳新县| 三原县| 潼关县| 闻喜县| 杭锦后旗| 洛阳市| 栾川县| 桃源县| 泰和县| 遂平县| 安图县| 镶黄旗|