2017-10-06 51 views
0

我正在做一個gridview謎題的鎖屏我已經成功地觸發了屏幕上的screenscreen和屏幕上當我只有在我的lockscrean按鈕時,我添加網格視圖在我的lockscreen活動不會來我屏幕上。添加gridview到鎖屏

下面是我的鎖屏服務,而不GridView控件:

public class LockscreenService extends Service { 
private LinearLayout linearLayout; 
private WindowManager.LayoutParams layoutParams; 
private WindowManager windowManager; 
@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 
BroadcastReceiver screenReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) && linearLayout == null) { 
      init(); 
     } 
    } 
}; 
@Override 
public void onCreate() { 
    super.onCreate(); 
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF); 
    registerReceiver(screenReceiver, intentFilter); 
    windowManager = ((WindowManager) getSystemService(WINDOW_SERVICE)); 
    layoutParams = new WindowManager.LayoutParams(
      WindowManager.LayoutParams.MATCH_PARENT, 
      WindowManager.LayoutParams.MATCH_PARENT, 
      WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, 
      WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 
        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION, 
      PixelFormat.TRANSLUCENT); 
} 
private void init() { 
    linearLayout = new LinearLayout(this); 
    windowManager.addView(linearLayout, layoutParams); 
    ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.lockscreen, linearLayout); 
// View btnClose = linearLayout.findViewById(R.id.button); 
    View btnClose = linearLayout.findViewById(R.id.button8); 
    btnClose.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // do something when the corky3 is clicked 
      windowManager.removeView(linearLayout); 
      linearLayout = null; 
     } 
    }); 
} 

它給出了一個鎖屏這樣lockscreen without gridview

,當我在初始化加鎖屏到的LinearLayout()methid這樣

public class LockscreenService extends Service { 
String p; 
ImageView myImage; 
final ArrayList<Bitmap> beforeshuffle = new ArrayList<>(9); 
final ArrayList<Bitmap> aftershuffle = new ArrayList<>(9); 
ArrayList<Bitmap> smallimages = new ArrayList<Bitmap>(9); 
GridView grid; 
Bitmap bs; 
Bitmap as; 

private LinearLayout linearLayout; 
private WindowManager.LayoutParams layoutParams; 
private WindowManager windowManager; 
@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 
BroadcastReceiver screenReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) && intent.getAction().equals(Intent.ACTION_SCREEN_ON)&& linearLayout == null) { 
      init(); 
     } 
    } 
}; 
@Override 
public void onCreate() { 
    Log.d("jhg","inservice"); 
    super.onCreate(); 
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF); 
    registerReceiver(screenReceiver, intentFilter); 
    windowManager = ((WindowManager) getSystemService(WINDOW_SERVICE)); 
    layoutParams = new WindowManager.LayoutParams(
      WindowManager.LayoutParams.MATCH_PARENT, 
      WindowManager.LayoutParams.MATCH_PARENT, 
      WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, 
      WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 
        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION, 
      PixelFormat.TRANSLUCENT); 
} 
private void init() { 
// Log.d("hdkjfh","in init"); 
    linearLayout = new LinearLayout(this); 
    windowManager.addView(linearLayout, layoutParams); 
    ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.lockscreen, linearLayout); 
    // View btnClose = linearLayout.findViewById(R.id.button); 
// grid = (GridView) linearLayout.findViewById(R.id.gridView); 
    // grid = (GridView) findViewById(R.id.gridView); 
    Globalvariable c = new Globalvariable(); 

    String p = c.givedata(); 
    Log.d("fg", p); 
    bs = c.giveBitmap(); 
    // Bitmap bitmap = (Bitmap) i.getParcelableExtra("bitmap"); 

    //smallimage_Numbers is to tell how many smallimage_s the image should split 

    int smallimage_Numbers = 9; 

    //Getting the source image to split 

    // ImageView image = (ImageView) findViewById(R.id.source_image); 
    // File imgFile = new File("profile.jpg" 

    Bitmap myBitmap = BitmapFactory.decodeFile(p); 
    //Drawable d = new BitmapDrawable(getResources(), myBitmap); 
    // myImage= (ImageView) linearLayout.findViewById(R.id.My); 
    // myImage = (ImageView) findViewById(R.id.My); 
    // myImage.setImageBitmap(myBitmap); 
    // myImage.setVisibility(View.INVISIBLE); 
    splitImage(myBitmap, smallimage_Numbers); 

    View btnClose = linearLayout.findViewById(R.id.button8); 

    btnClose.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // do something when the corky3 is clicked 
      Bitmap as = getBitmapFromView(grid); 

      if (bs.sameAs(as)) { 
       Toast.makeText(getApplicationContext(), "CORRECT", Toast.LENGTH_SHORT).show(); 
       windowManager.removeView(linearLayout); 
       linearLayout = null; 
      } else { 
       Toast.makeText(getApplicationContext(), "WRONG", Toast.LENGTH_SHORT).show(); 
      } 

     } 
    }); 
} 


    //invoking this method makes the actual splitting of the source image to given number of smallimage_s 


    //Image.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250)); 


    /** 
    * Splits the source image and show them all into a grid in a new activity 
    * 
    * @param 
    * @param smallimage_Numbers The target number of small image smallimage_s to be formed from the source image 
    */ 

public void splitImage(Bitmap bit, int smallimage_Numbers) { 


    //For the number of rows and columns of the grid to be displayed 

    int rows, cols; 


    //For height and width of the small image smallimage_s 

    int smallimage_Height, smallimage_Width; 


    //To store all the small image smallimage_s in bitmap format in this list 

    smallimages = new ArrayList<Bitmap>(smallimage_Numbers); 


    //Getting the scaled bitmap of the source image 

    // BitmapDrawable mydrawable = (BitmapDrawable) image.getDrawable(); 

    Bitmap bitmap = bit; 

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true); 

    rows = cols = (int) Math.sqrt(smallimage_Numbers); 

    smallimage_Height = bitmap.getHeight()/rows; 

    smallimage_Width = bitmap.getWidth()/cols; 


    //xCo and yCo are the pixel positions of the image smallimage_s 

    int yCo = 0; 

    for (int x = 0; x < rows; x++) { 

     int xCo = 0; 

     for (int y = 0; y < cols; y++) { 

      smallimages.add(Bitmap.createBitmap(scaledBitmap, xCo, yCo, smallimage_Width, smallimage_Height)); 

      xCo += smallimage_Width; 

     } 

     yCo += smallimage_Height; 

    } 
    Array[] in = new Array[9]; 


    //Collections.shuffle(smallimages); 
    grid.setAdapter(new ImageAdapter(this, smallimages)); 
    grid.setNumColumns((int) Math.sqrt(smallimages.size())); 

    Collections.shuffle(smallimages); 
    grid.setAdapter(new ImageAdapter(this, smallimages)); 
    grid.setNumColumns((int) Math.sqrt(smallimages.size())); 
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     int counter = 0; 
     int firstclick; 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      counter++; 
      if (counter % 2 == 0) { 
       firstclick = position; 
       Bitmap data1 = smallimages.get(position); 
      } else { 
       Bitmap swapImage = smallimages.get(position); 
       smallimages.set(position, smallimages.get(firstclick)); 
       smallimages.set(firstclick, swapImage); 
       grid.invalidateViews(); 
      } 
      for (int i = 0; i < smallimages.size(); i++) { 
       aftershuffle.add(smallimages.get(i)); 
      } 
     } 

    }); 

    //activity to show these smallimage_s into a grid 

} 


//to compare two bitmaps in gridview 

{ 
    @Override 
    public void onClick (View v){ 
    Bitmap as = getBitmapFromView(grid); 

    if (bs.sameAs(as)) { 
     Toast.makeText(getApplicationContext(), "CORRECT", Toast.LENGTH_SHORT).show(); 
     windowManager.removeView(linearLayout); 
     linearLayout = null; 
    } else { 
     Toast.makeText(getApplicationContext(), "WRONG", Toast.LENGTH_SHORT).show(); 
    } 
} 
}); 
*/ 
    public static Bitmap getBitmapFromView(View view){ 
    Bitmap bitmap =Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888); 
    Canvas canvas =new Canvas(bitmap); 
    Drawable bgDrawable = view.getBackground(); 
    if (bgDrawable!=null) 
     bgDrawable.draw(canvas); 
    else 
     canvas.drawColor(Color.WHITE); 
    view.draw(canvas); 

    return bitmap; 
} 

} 

當我點擊屏幕時,它不會返回任何內容。幫助我在我的鎖屏中顯示我的洗牌網格視圖。

回答

0

我沒有找到你引用GridView「grid」的地方。這兩條線是否有意見?你也可以提供ImageAdapter的實現嗎?