2016-07-17 1 views
0

내 애플리케이션에서 스크롤 메뉴가 있고이 기능을 전체 메뉴 항목에 사용합니다. 하지만 대부분의 시간 동안 나는 기억 상실증에서 빠져 나옵니다. 내 에뮬레이터에서 오류가 발생하지 않습니다. 그러나 내 장치는 항상 실패합니다. 나는 드로어 블로드를 효율적으로 많은 메모리를 사용하지 않음

android:hardwareAccelerated="false" 
    android:largeHeap="true" 

을 시도하고 이미지가 너무 크기를 감소. 그러나 장치는 너무 많은 메모리가 필요하다고 말합니다. 내 앱이 더 빠른 로딩과 효율적인 메모리를 원한다. 나는 아직도 안드로이드의 선발 투수이다. 이미지를 효율적으로로드하는 방법에 대한 기사를 보았습니다. 그러나 나는 이해하지 못했다. 누구든지 샘플 코드를 가지고 나를 도울 수 있습니까?

public void RefreshMenuItems(int category) { 
    //Create menu scroller 
    LinearLayout.LayoutParams card_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT); 
    card_lp.setMargins(dp(5), 0, dp(5), 0); 

    LinearLayout.LayoutParams image_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0); 
    image_lp.setMargins(dp(40), dp(5), dp(40), dp(5)); 
    image_lp.weight = 0.5f; 

    LinearLayout.LayoutParams desc_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0); 
    desc_lp.weight = 0.32f; 

    LinearLayout.LayoutParams title_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0); 
    title_lp.weight = 0.08f; 

    RelativeLayout.LayoutParams price_rp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    price_rp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 

    RelativeLayout.LayoutParams buy_rp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    buy_rp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 

    LinearLayout.LayoutParams bottom_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0); 
    bottom_lp.weight = 0.1f; 

    //fill menu 
    for (int i = 0; i < item_title.length; i++) { 
     final int currenti = i; 

     if ((item_category[i] == category) || category == 0) { 
      TextView title = new TextView(this); 
      title.setText(item_title[currenti]); 
      title.setGravity(Gravity.CENTER); 
      title.setTextColor(0xff212121); 
      title.setPadding(dp(10), dp(0), dp(10), dp(0)); 
      title.setTypeface(null, Typeface.BOLD); 
      title.setLayoutParams(title_lp); 
      title.setBackgroundResource(R.drawable.borderradius2); 
      title.setTextSize(fontpercent_screenheight(2.5)); 

      ImageView item = new ImageView(this); 
      item.setImageResource(getResources().getIdentifier(item_image[i], "drawable", getPackageName())); 
      item.setScaleType(ScaleType.FIT_CENTER); 
      item.setAdjustViewBounds(true); 
      item.setLayoutParams(image_lp); 

      TextView desc = new TextView(this); 
      desc.setText(item_shortdesc[currenti]); 
      desc.setPadding(dp(10), dp(5), dp(10), dp(5)); 
      desc.setLayoutParams(desc_lp); 
      desc.setTextSize(fontpercent_screenheight(2.5)); 

      TextView price = new TextView(this); 
      price.setText(String.format(sign + "%.2f", Double.parseDouble(item_price[currenti]))); 
      price.setPadding(0, dp(5), dp(10), dp(5)); 
      price.setLayoutParams(price_rp); 
      price.setTextSize(fontpercent_screenheight(2.5)); 

      buy = new TextView(this); 
      buy.setText(R.string.MenuAddToOrderButton); 
      buy.setPadding(dp(10), dp(5), 0, dp(5)); 
      buy.setTextColor(0xffff0000); 
      buy.setLayoutParams(buy_rp); 
      buy.setTextSize(fontpercent_screenheight(2.5)); 
      buy.setClickable(true); 
      buy.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        addItemToOrder(currenti); 
       } 
      }); 
      buy.setOnTouchListener(new OnTouchListener() { 
       @Override 
       public boolean onTouch(View v, MotionEvent event) { 
        if (event.getAction() == MotionEvent.ACTION_DOWN) { 
         ((TextView) v).setTextColor(0xff212121); 
        } else if (event.getAction() == MotionEvent.ACTION_UP) { 
         ((TextView) v).setTextColor(0xffff0000); 
        } 
        return false; 
       } 
      }); 

      RelativeLayout bottom = new RelativeLayout(this); 
      bottom.setLayoutParams(bottom_lp); 
      bottom.addView(buy); 
      bottom.addView(price); 

      LinearLayout card = new LinearLayout(this); 
      card.setOrientation(LinearLayout.VERTICAL); 
      card.setLayoutParams(card_lp); 
      card.setBackgroundResource(R.drawable.borderradius1); 
      card.setWeightSum(1f); 
      card.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent intent = new Intent(context, ItemProfile.class).putExtra("item_image", getResources().getIdentifier(item_image[currenti], "drawable", getPackageName())).putExtra("item_title", item_title[currenti]).putExtra("item_desc", item_desc[currenti]).putExtra("item_price", Double.parseDouble(item_price[currenti])); 
        startActivity(intent); 
       } 
      }); 

      card.addView(title); 
      card.addView(item); 
      card.addView(desc); 
      card.addView(bottom); 

      menu.addView(card); 
     } 
    } 
} 
+0

내 대답보기. –

답변

0

언제든지 수동으로보기를 만드는 코드를 반복해서 보게 될 때가 있습니다. 나는 보통 상황이 최적의 방식으로 수행되고 있지 않다는 것을 걱정합니다. https://developer.android.com/guide/topics/ui/declaring-layout.html을 다시 살펴보십시오. ListView를 살펴보고 싶을 수도 있습니다. 직접 작성하려고하는 것처럼 보입니다.

+0

나는 당신을 얻지 않는다. –

+0

메뉴 항목을 포함하는 메뉴를 만드는 것처럼 보입니다. 보기를 직접 만들어서 안드로이드에 목록을 작성해서는 안됩니다. ListView와 어댑터를 사용해야합니다. –

+0

어떻게 어댑터로 변경할 수 있습니까? –

관련 문제