久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

android中實現(xiàn)懸浮窗口并滾動

 android之情殤 2013-01-18

android中實現(xiàn)懸浮窗口并滾動

(2012-08-27 11:36:55)
標(biāo)簽:

it

因為項目需要最近研究了android中實現(xiàn)懸浮并滾動的效果,筆記如下。

首先要實現(xiàn)一個自己畫的滾動View.

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class AutoScroll extends TextView {
  private float textLength = 0f;//文本長度
  private float viewWidth = 0f;
  private float step = 0f;//文字的橫坐標(biāo)
  private float y = 0f;//文字的縱坐標(biāo)
  private float temp_view_plus_text_length = 0.0f;//用于計算的臨時變量
  private float temp_view_plus_two_text_length = 0.0f;//用于計算的臨時變量
  public boolean isStarting = false;//是否開始滾動
  private Paint paint = null;//繪圖樣式
  private String text = "";//文本內(nèi)容
  Canvas acanvas;
 
 
  private Handler handler = new Handler()
     {
      @Override
   public void handleMessage(Message msg)
      {
       onDraw(acanvas);
   }
      
     };
 public AutoScroll(Context context)
 {
  super(context);
//  initView();
 }
 public AutoScroll(Context context, AttributeSet attrs)
 {
  super(context, attrs);
//  initView();
 }
 public AutoScroll(Context context, AttributeSet attrs, int defStyle)
 {
  super(context, attrs, defStyle);
//  initView();
 }
 
// public void onClick(View v) {
//   if(isStarting)
//             stopScroll();
//         else
//             startScroll();
// }
//  private void initView()
//     {
//         setOnClickListener(this);
//     }
  public void init(WindowManager windowManager)
     {
         paint = getPaint();
         text = getText().toString();
         textLength = paint.measureText(text);//textview中字?jǐn)?shù)的長度
         viewWidth = getWidth();
         if(viewWidth == 0)
         {
             if(windowManager != null)
             {
                 Display display = windowManager.getDefaultDisplay();
                 viewWidth = display.getWidth();
             }
         }
         step = textLength;
         temp_view_plus_text_length = viewWidth + textLength;
         temp_view_plus_two_text_length = viewWidth + textLength * 2;
         y = getTextSize() + getPaddingTop();
     }
     public void startScroll()
     {
         isStarting = true;
         invalidate();
     }
   
   
     public void stopScroll()
     {
         isStarting = false;
         invalidate();
     }
     public void onDraw(Canvas canvas) {
      acanvas = canvas;
         canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
         if(!isStarting)
         {
             return;
         }
         step += 2;//0.5為文字滾動速度。
         if(step > temp_view_plus_two_text_length)
             step = textLength;
   invalidate();
     }

}

在xml文件中將其畫出來,。  

 <com.jzby.AutoScroll
    android:id="@+id/TextViewNotice"
    android:layout_height="30px"

    android:layout_width="fill_parent"
    android:text="我是OSD,我要開始滾動啦,,哦也"
    android:textColor="#ffffffff"
    android:inputType="text"
    android:background="#eee"
    android:textSize="20px"
    android:layout_marginTop="50px"
   />

至此滾動效果實現(xiàn),,接下來要實現(xiàn)懸浮窗的效果需要在service中啟動懸浮窗。

package com.jzby;


import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class MyService extends Service{

  private View view;// 透明窗體
     private boolean viewAdded = false;// 透明窗體是否已經(jīng)顯示
     private WindowManager windowManager;
     private WindowManager.LayoutParams layoutParams;
        private AutoScroll auto;
     @Override
     public IBinder onBind(Intent intent)
     {
         return null;
     }
     @Override
     public void onCreate() {
         super.onCreate();
         view = LayoutInflater.from(this).inflate(R.layout.autoscroll, null);

         windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
        
         layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
                 LayoutParams.WRAP_CONTENT, LayoutParams.TYPE_SYSTEM_ERROR,
                 LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
         //layoutParams.gravity = Gravity.RIGHT|Gravity.BOTTOM; //懸浮窗開始在右下角顯示
         layoutParams.gravity = Gravity.RIGHT | Gravity.TOP;
         auto=(AutoScroll)view.findViewById(R.id.TextViewNotice);
         auto.setText("測試測試測試測試");
     }

    
     private void refresh() {
         if (viewAdded) {
             windowManager.updateViewLayout(view, layoutParams);
         } else {
          //layoutParams.y = 300;
             windowManager.addView(view, layoutParams);          
             viewAdded = true;
             AutoScroll autoScrollTextView = (AutoScroll)view.findViewById(R.id.TextViewNotice);
             autoScrollTextView.init(windowManager);
             autoScrollTextView.startScroll();
         }
     }
     @Override
     public void onStart(Intent intent, int startId) {
         super.onStart(intent, startId);
         refresh();
     }

    
     public void removeView() {
         if (viewAdded)
         {
             windowManager.removeView(view);
             viewAdded = false;
         }
     }

     @Override
     public void onDestroy()
     {
         super.onDestroy();
         removeView();
         Log.d("CCtext", "ONDESTORY");
     }
}
綜上在mainfestview中再配置好狀態(tài)即可

<!-- 懸浮窗(FloatingService) permission -->
      <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多